Understanding Ownership Flashcards

1
Q

How does Rust handle memory management?

A

Through a system of ownership with a set of rules that the compiler checks at compile time. None of the ownership features slow down your program while it’s running.

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

Why is pushing to the Stack faster than allocating to the Heap?

A

On the Stack, the OS does not have to search for the data. Allocating space on the heap requires more work, because the OS must first find a big enough space to hold the data and then perform bookkeeping to prepare for the next allocation.

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

What are some problems that ownership addresses?

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
4
Q

What are the three ownership rules in 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
5
Q

When does memory become free?

A

When a variable that owns it goes out of scope.

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

When a variable goes out of scope, what special function is called by Rust?

A

drop, it is called automatically at a closing curly brace (Resource Acquisition Is Initialization (RAII)).

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

What is a move in Rust?

A

Transferring ownership of resources from one reference to another and invalidating the first reference.

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

What does the clone method do?

A

Performs a deep copy of a variable and value from Stack and Heap.

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

How is the Copy trait used?

A

Annotates types that are stored entirely on the Stack. If a type is annotated with Copy, an older variable is still usable after assignment.

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

What is the pattern the value of a variable follows everytime?

A

Assigning the value to another variable moves it.

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

When will a value be cleaned up by Drop?

A

When a variable that includes data on the heap goes out of scope, unless the value has been moved to be owned by another variable.

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

What does the & represent?

A

A reference.

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

What does a reference do?

A

Allow reference to some value without taking ownership of it.

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

What is borrowing in Rust?

A

Having references as function parameters. When an function is passed a reference to a value.

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

Are references mutable by default?

A

No.

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

How many mutable references are allowed on a particular piece of data?

A

Exactly one.

17
Q

What is the benefit of allowing only one mutable reference for a particular piece of data?

A

Allows for prevention of data races at compile time.

18
Q

When does a data race occur?

A

when these three behaviors occur:

  1. Two or more pointers access the same data at the same time.
  2. At least one of the pointers is being used to write to the data.
  3. There’s no mechanism being used to synchronize access to the data.
19
Q

Does Rust allow for simultaneous mutable and immutable references?

A

No.

20
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 memory while preserving a pointer to that memory.

21
Q

How does Rust prevent dangling pointers?

A

If there is 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.

22
Q

What are the two rules for references?

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.
23
Q

Do Slice data types have ownership?

A

No.

24
Q

What do Slices allow for?

A

Referencing a contiguous sequence of elements in a collection rather than the whole collection.

25
Q

How do we create a Slice?

A

Using a range within brackets by specifying [starting_index..ending_index], where starting_index is the first position in the slice and ending_index is one more than the last position in the slice.

26
Q

What is Rust’s range syntax?

A

..