Rust Book Day 3 Flashcards
Rust learning
let s1 = s2; does what if its a string vs i32 ?
If it is a string, s1 gets dropped from memory. If it is an i32 we keep two pairs of data.
how can you create both s1 and s2 in case of string ?
use let s2 = s1.clone()
why are both still valid after this statement?
let integer1 = integer2;
this is possible because these types implement the Copy trait, meaning they are on the stack and a copy is fast (trivial) to make, so they are being done.
when passing a string into a function, will it be still available outside the function
no
when passing a i32 to a function, will it be still available outside the function
yes
can you do this ?
let mut s = String::from(“hello”);
let r1 = &s; let r2 = &s; println!("{r1} and {r2}"); let r3 = &mut s; println!("{r3}");
yes, because variables r1 and r2 will not be used after they have been printed. this allows for &mut s to be possible (only 1 &mut reference can exist at a given time)