CH 4. Understanding Ownership. Flashcards

1
Q

What is the ownership model used for?

A

Its used as a way to manage memory.

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

What is the stack?

A

The stack is a fixed sized memory allocation that cannot grow or shrink during runtime. The stack also stores stack frames which stores function/variables and their size, the size is calculated at compile time. So variables must have a known size at compile time.

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

What is the heap?

A

The heap is a dynamic sized memory allocation that can change throughout runtime. Memory used in the heap have lifetimes which we can control to determine how long memory is used for and when to discard it.

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

Is is faster to allocate/retrieve data from the stack or the heap and why?

A

The stack is faster because this memory is known and has memory already allocated for it, whereas the heap will need to look for open space to allocate memory for. When retrieving data the heap will have to follow a pointer whereas the stack will have the memory needed.

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

What are the three core ownership rules for Rust?

A
  1. Each value in Rust has a variable that’s called its owner.
  2. There can only be one owner at a time.
  3. When the owner goes out of scope, the value will be dropped.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a move in Rust?

A

A move is when you try to copy a value which is stored on the heap, because rather than copy the value which would take up more memory, the values instead move in ownership for that value.

let x: i32 = 5;
let y: i32 = x; // Copy

// Remember String is stored dynamically
let s1: String = String::from(“Hello”);
let s2: String = s1; // Move

// This next line cant compile because s1 has been dropped from memory since
// its value moved in ownership to s2
println!(“{}, world!”, s1;

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

What determines if a functions passed in parameter values will be copied or moved?

A

If the passed in values are stored on the heap or on the stack.

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

Are returned values from a function moved or copied?

A

They are moved.

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

What is a reference?

A

A reference is a pointer that wont take ownership of a value when used. In rust a pointer is indicated by the & key.

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

Are references mutable?

A

Not by default, but they can be.

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

What is the scope of a reference?

A

From when it is first declared to when it is last used.

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

What is a dangling reference?

A

It is a reference that points to invalid data.

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

What are the two rules of references in Rust?

A
  1. At any given time, you can have either one mutable reference or any number of immutable references.
  2. References must always be valid.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are slices?

A

References to a continuous sequence of elements within a collection.

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

Do slices take ownership of the data?

A

No

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