Ownership Flashcards

1
Q

How does the stack store values?

A

In the order it gets them and removes the values in the opposite order.

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

Why is the stack fast?

A

It never has to search for it because it is always at the top. The data on the stck must also take up a known fixed size.

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

What data is suitable to be stored on the heap?

A

Data with a size unknown at compile time or a size that might change.

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

Is pushing data on the stack considered allocating?

A

No

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

Why is accessing data on the heap slower than on the stack?

A

Because you have to follow a pointer to get to the location.

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

When you call a function, the values passed into the function (including pointers to data on the heap) and the functions local variables get pushed onto the ____.

A

Stack

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

What does ownership address?

A
  1. Keeping track of what parts of code are using what data on the heap
  2. Minimizing the amount of duplicate data on the heap
  3. Cleaning up unused data on the heap so you don’t run out of space
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why does the concept of ownership exist?

A

To manage heap data.

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

What are the three rules of ownership in rust?

A
  1. Each value in rust has a variable that’s called its owner.
  2. There can be only 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
10
Q

What is the scope of an item?

A

A scope is a range within a program for which an item is valid.

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

Why are string literals fast and efficient?

A

The text is hard coded directly into the executable.

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

Since rust doesn’t use a garbage collector, how does it keep track and clean up memory?

A

Memory is automatically returned once the variable that owns it goes out of scope.

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

What is the name of the function that is called when a variable goes out of scope?

A

Drop

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

How is a String stored on the stack?

A

Ptr |*
Len |
Capacity|

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

If s1 and s2 both point to the same content on the heap, what happens when both s1 and s2 go out of scope? Is this allowed in rust?

A

They will both try to free the same memory. This is known as double free error. Rust will invalidate the first variable when the second is assigned to it. This is called a move (similar to a shallow copy).

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

Will rust ever automatically create a deep copy of data on the heap?

A

No, a deep copy must explicitly call the clone method.

17
Q

How do you know if some arbitrary code is being executed and that code may be expensive?

A

It’s calling the clone method.

18
Q

When is a items stored on the stack?

A

Items that have a known size at compile time are stored on the stack. In this case there is no difference between a shallow and deep copy and a copy will not invalidate the original variable.

19
Q

If an older variable is still usable after assignment then the type must use which trait?

A

Copy trait

20
Q

You cannot annotate a type with the Copy trait if the type, or any of its parts, has implemented which trait?

A

Drop

21
Q

True or False: passing a variable to a function will move or copy m, just as assignment does.

A

True

22
Q

Describe the pattern of ownership.

A
  1. Assigning a value to another variable moves it.
  2. When a variable that includes data on the heap goes out of scope the value will be cleans up by drop unless the data has been moved to be owned by another variable.
23
Q

What do references allow you to do?

A

They allow you to refer to some value without taking ownership of it.

24
Q

If you have a reference to a variable will the item being referenced be dropped when the reference goes out of scope?

A

No

25
Q

What do you call references as functional parameters?

A

Borrowing

26
Q

What happens if you try to modify something that is borrowed?

A

It won’t work. You need a mutable reference.

27
Q

What is the main restriction of a mutable reference?

A

You can have only one mutable reference to a particular piece of data in a particular scope.

28
Q

What is the benefit of restricting mutable references?

A

Rust can prevent data races at compile time.

29
Q

Why are multiple immutable references ok?

A

Because no one who is reading the data has the ability to affect anyone else’s reading of the data.

30
Q

Can you have a single mutable reference with multiple immutable references?

A

No

31
Q

What is a dangling pointer?

A

A pointer that references a location in memory that may have been given to someone else, by freeing some some memory while preserving a pointer to that memory.

32
Q

How does rust ensure that there will be no dangling pointers?

A

If you have a reference to some data the compiler will ensure that the data will not go out of scope before the reference to the data does.

33
Q

What are the rules of references?

A
  1. At any time you can have either but not both of the of the following: one mutable reference or any number of immutable references
  2. References must always be valid
34
Q

What does a slice enable you to do?

A

Let you reference a contiguous sequence of elements in a collection rather than the whole collection.

35
Q

list the common issues that ownership solves:

A
  1. Double free error - solved by moving
  2. Data races - restriction on mutable references
  3. Dangling pointers - data will not go out of scope before the reference to the data does