Rust Book Day 3 Flashcards

Rust learning

1
Q

let s1 = s2; does what if its a string vs i32 ?

A

If it is a string, s1 gets dropped from memory. If it is an i32 we keep two pairs of data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how can you create both s1 and s2 in case of string ?

A

use let s2 = s1.clone()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

why are both still valid after this statement?
let integer1 = integer2;

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

when passing a string into a function, will it be still available outside the function

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

when passing a i32 to a function, will it be still available outside the function

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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}");
A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly