Fundamentals Flashcards

1
Q

Name the rules of ownership.

A
  1. Each value has a variable called its owner.
  2. There can only be one owner at a time.
  3. When an 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
2
Q

List all built-in copy types.

A
  • booleans
  • all integers
  • all floats
  • tuples of copy types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List the rules of references.

A
  1. At any given time, you can have either but not both of:
    • One mutable reference
    • 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
4
Q

List all available enum variants.

A
  • unit like (no arguments)
  • tuple like (positional arguments)
  • struct like (named arguments)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List the module rules.

A
  1. If module foo has no submodules put its definitions in a file named foo.rs
  2. If foo has submodules put its definitions in foo/mod.rs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List the privacy rules.

A
  1. pub items can be accessed by any parent module.
  2. everything else can only be accessed by their immediate parent module and all child modules.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Types allowing shared ownership.

A
  • std::rc::RC / std::rc::Weak
  • std::sync::Arc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do Rc/Weak and Arc work?

A
  • By implementing the Clone, Drop and Deref traits
    1. .clone() increments reference count
    2. .drop() decrements reference count
    3. other method calls are delegated to their contained value
    4. their own functionality is implemented as associated functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Name two types that all enable interior mutability.

A

Cell & RefCell

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

How can you access a copy of the value contained in a Cell?

A

fn get(&self) -> T

with T: Copy

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

How can you change the value contained in a Cell?

A

fn set(&self, val: T)

with T: Copy

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

What is the difference between a Cell and a RefCell?

A

Cell does not let you call mut methods on it’s contained values and its values need to implement the Copy trait.

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

How can you get access to the value contained in a std::cell::RefCell?

A
  • fn borrow(&self) -> Ref<t></t>
  • fn try_borrow(&self) -> Result<ref>, BorrowError&gt;</ref>
  • fn borrow_mut(&self) -> RefMut<t></t>
  • fn try_borrow_mut(&self) -> Result<refmut>, BorrowMutError&gt;</refmut>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens when the rules of reference are violated with the help of a RefCell?

A

The borrow and borrow_mut methods panic.

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

How is the RefMut type used?

A

RefMut can be used as a mutable reference of the contained type.

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

How can you spawn a new thread?

A

std::thread::spawn(|| { println!(“hello”); })

the argument is a FnOnce closure