fn main() { println!("Hello, world!"); my_funtion(); println!("{}", with_parameters(12, 13)); let mut string: String = String::from("hello"); string.push_str(" world"); println!("{string}"); let string2 = string; // This no longer works because string2 now owns it and not string // let length = calculate_length(&string); let length = calculate_length(&string2); { let _string3 = string2; } // This no longer works as the value shifted to _string3 and _string3 was dropped // let length = calculate_length(&string2); println!("{length}"); let mut _x: i32 = 5; let _r: &mut i32 = &mut _x; *_r += 1; // This will give an error // the r was a mutable reference // there can only be one mutable reference // so both _x and _r are mutable references to _x // this gives an error // println!("{_x}"); println!("{_r}"); let mut account = BankAccount { owner: "Anand".to_string(), balance: 10000.64, }; // Immutable borrow to check balance account.check_balance(); // Mutable borrow to withdraw money account.withdraw(12.3); // Immutable borrow to check balance account.check_balance(); let hours = 3; println!("{}", hours * HOURS_TO_SECONDS); let bs = "bbbbb"; println!("{}", bs); let bs = bs.len(); println!("{}", bs); let age = 11; if age >= 18 { println!("You can drive"); } else if age >= 10 { println!("That's a lie"); } else { println!("You cannot drive"); } let condition = true; let inline = if condition { println!("woah"); 15 } else { 6 }; println!("{}", inline); // Do not do this // loop { // println!("hello world"); // } let mut counter = 0; let result = loop { counter += 1; if counter == 10 { break counter * 2; } }; println!("{}", result); // Loops can also have labels let mut count = 0; 'counting_up: loop { println!("count = {count}"); let mut remaining = 10; loop { println!("remaining = {remaining}"); if remaining == 9 { break; } if count == 2 { break 'counting_up; } remaining -= 1; } count += 1; } while count <= 10 { println!("count = {count}"); count += 1; } let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; for element in a { println!("{}", element * 2); } } const HOURS_TO_SECONDS: i32 = 60 * 60; struct BankAccount { owner: String, balance: f64, } impl BankAccount { fn withdraw(&mut self, amt: f64) { println!("Withdrawing {} from {}.", amt, self.owner); self.balance -= amt; } fn check_balance(&self) { println!( "Account owned by {} has balance of {}", self.owner, self.balance ); } } fn my_funtion() { println!("Hi there"); } fn with_parameters(a: i32, b: i32) -> i32 { a + b } fn calculate_length(s: &String) -> usize { s.len() }