ownership Flashcards

understand the principals of ownership

1
Q

The exact lifetime of each value is controlled by the programmer. Yes or No?

A

Yes.

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

Will the compiler check the lifetime of values?

A

yes.

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

Does the Rust ownership mechanism prevent dangling pointers?

A

yes.

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

Does rust provide a nil value?

A

No.

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

In rust, every value has exactly __ owner

A

one.

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

when the owner is freed, the value is __

A

dropped.

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

Does Rust use a Garbage Collector?

A

No

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

T or F. Each value in rust has a variable that is its owner.

A

True. Except rc

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

What are the three ownership rules?

A
  1. Each value has exactly one owner
  2. Owners can change, but there can only be one at any instant.
  3. When the owner goes out of scope, the value is dropped.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the restriction for mutable references?

A

There can be only one mutable reference. There cannot be a mix of mutable and unmutable

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

What must references always be?

A

Valid. The must always reference something.

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

What is the syntax for making a string slice?

A
let s = “test”.to_string();
let slice = s[..3]; //first three bytes
//0,1,2 chars were copied. Not 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the Rust concept of Move

A

Moving ownership. Assignment will move ownership.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
let x = 3; 
let y = 3; 
println!("{}", x) // will this work? why ? 
let x = "blaster".to_string()
y = x 
println!("{}", x) // will this work? why ?
A

printing the integer will work because integers implement Copy trait. This means they aren’t moved by assignment, but copied on the stack. However, std::string types aren’t copied, they are moved. The second print won’t compile.

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