Some Rust notes
- io::stdin().read_line(&xx) appends new value on the target which is “xx” in this case.
- A tuple can have values with different types in it while an array must have the same type.
- A tuple with explictly type annotations example:
let x: (i32, f64, u8) = (500, 6.4, 1);
- An array in Rust have a fixed length while a Vector can grow or shrink its size.
- An array with explictly type and elements’ number annotations example:
let a: [i32; 5] = [1, 2, 3, 4, 5];
- If [6, 6, 6, 6, 6] is the desirable array, we can declare it in this way:
let a = [6; 5];
- A good way to create a new instance of a struct that uses most of an old instance’s values but changes some is using update syntax:
let user2 = User { email: String::from("user2@example.com"), username: String::from("user2name"), ..user1 };
- An example of using derived traits of custom structs:
#[derive(Debug)] println!("{:?}",user1); println!("{:#?}",user1);
- If a struct tuple is defined, when a function want to call an instance of this struct, other struct tuple (even with the same form) cannot be used:
struct Color (u32,u32,u32); struct Point (u32,u32,u32);
In this case, if a function want to call an instance of “Color”, we cannot put an instance of “Point” in it even they have the same form.
- An example of a struct and its method implementation: ```rust struct Rectangle { width: u32, height: u32, } impl Rectangle { fn area(&self) -> u32 { self.width * self.height }