fn main() { let guess: u32 = "42".parse().expect("Not a number!"); println!("You guessed: {}", guess); let heart_eyed_cat = '😻'; println!("{}", heart_eyed_cat); let cc = another_function(3, 'a'); println!("The value of cc is: {}", cc); // Statements and Expressions let x = { let y = 5; y + 1 }; println!("The value of x is: {}", x); for number in 1..4 { println!("{}!", number); } let result = fibonacci_function(3); println!("The result is: {}", result); // 相互转换摄氏与华氏温度 let celsius = 30.0; let fahrenheit = celsius_to_fahrenheit(celsius); println!("{} celsius is {} fahrenheit.", celsius, fahrenheit); let fahrenheit = 86.0; let celsius = fahrenheit_to_celsius(fahrenheit); println!("{} fahrenheit is {} celsius.", fahrenheit, celsius); let mut s = String::from("hello"); s.push_str(", world!"); println!("{}", s); let s2 = s; println!("{}", s2); // println!("{}", s); let t = String::from("hello"); takes_ownership(t); // println!("{}", t); let y = 5; makes_copy(y); println!("{}", y); let s4 = String::from("hello"); let len = calculate_length(&s4); println!("The length of '{}' is {}.", s4, len); // let s5 = String::from("hello"); // change(&s5); let mn = String::from("hello world"); let word = first_word(&mn); // mn.clear(); println!("The first word is: {}", word); // let width1 = 30; // let height1 = 50; let rect1 = Rectangle { width: 30, height: 50, }; dbg!(&rect1); // println!("rect1 is {:#?}", rect1); // println!( // "The area of the rectangle is {} square pixels.", // area(&rect1) // ); println!( "The area of the rectangle is {} square pixels.", rect1.area() ); let rect2 = Rectangle::square(3); println!( "The area of the rectangle is {} square pixels.", rect2.area() ); } fn another_function(a: i32, char: char) -> i32 { println!("Another function."); println!("The value of a is: {}", a); println!("The value of char is: {}", char); 5 } fn fibonacci_function(n: i32) -> i32 { if n == 0 { return 0; } else if n == 1 { return 1; } else { return fibonacci_function(n - 1) + fibonacci_function(n - 2); } } fn celsius_to_fahrenheit(celsius: f64) -> f64 { celsius * 1.8 + 32.0 } fn fahrenheit_to_celsius(fahrenheit: f64) -> f64 { (fahrenheit - 32.0) / 1.8 } fn takes_ownership(some_string: String) { // some_string 进入作用域 println!("{some_string}"); } // 这里,some_string 移出作用域并调用 `drop` 方法。 // 占用的内存被释放 fn makes_copy(some_integer: i32) { // some_integer 进入作用域 println!("{some_integer}"); } // 这里,some_integer 移出作用域。没有特殊之处 fn calculate_length(s: &String) -> usize { s.len() } // fn change(some_string: &String) { // some_string.push_str(", world"); // } fn first_word(s: &String) -> &str { let bytes = s.as_bytes(); for (i, &item) in bytes.iter().enumerate() { if item == b' ' { return &s[..i]; } } &s[..] } // fn area(width: u32, height: u32) -> u32 { // width * height // } #[derive(Debug)] struct Rectangle { width: u32, height: u32, } impl Rectangle { fn area(&self) -> u32 { self.width * self.height } } impl Rectangle { fn square(size: u32) -> Rectangle { Rectangle { width: size, height: size, } } }