Rust Language Flashcards

1
Q

What are the rust ownership rules ?

A
  • Each value in Rust has an owner.
  • There can only be one owner at a time.
  • 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
2
Q

What is the function of the memory allocator in Rust ?

A

It provisions memory on the heap, which is where variable sized data is stored. And is also responsible for doing housekeeping and deallocating memory for subsequent use.

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

Can you explain the drop function and how it works ?

A

The drop function works like a hook, it allows the author who requested memory from the allocator, place code which returns the allocated memory. It is called automatically by rust at the end of every curly brace.
Read more here:
Note: In C++, this pattern of deallocating resources at the end of an item’s lifetime is sometimes called Resource Acquisition Is Initialization (RAII). The drop function in Rust will be familiar to you if you’ve used RAII patterns.

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

Can you explain the difference between variable move and copy in Rust ?

A

When we bind a variable to a value and bind the bound variable to another, rust looks at where the variable is allocated to in memory to know if it should do a move or a copy. Fixed sized data which are stack allocated are simply copied, for dynamically sized data Rust does a move instead, where it invalidates the first variable.

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

How does Rust know when to do a move or a copy ?

A

It does this through 2 annotations, the Copy trait and the Drop trait. Types that implement the Copy trait are stack allocated and have their data simply copied, while types that implement the Drop trait are heap allocated and have their data moved.

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

Why does moving a reference to a String (&String) result in copying fewer bytes than moving a String ?

A

Because the String type consists of a pointer, a length and a capacity, while the &String consists of the pointer alone.

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

What are two reference rules when dealing with Rust ?

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

What are associated functions in Rust and what are the two types ?

A

Associated functions are similar to methods in java. They are functions implemented on a particular struct or type.
They are of two types, associated functions that reference self which is a reference to the type a function is implemented on and those that don’t, similar to static methods on a class in java

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

In what case would you move a reference to self in an implementation method, as opposed to borrowing it.

A

This technique is usually used when the method transforms self into something else and you want to prevent the caller from using the original instance after the transformation.

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

What are Generic Types ?

A

They are abstract stand in types, for concrete types or properties. They allow a programmer write, functions that can be used with any type.

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

Is there a performance penalty with using generics ? If not how does rust accomplish this ?

A

There’s no runtime diff between code that runs with generics and code that uses concrete types. This is because rust performs Monomorphiziation at compile time.

Monomorphization is the process of turning generic code into concrete code, by filling in the concrete types the code was compiled with.

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

What is the use case for Generics ?

A

Generics help reduce code duplication by allowing the programmer write code, that can be used for any type T

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

What are Trait bounds and why are they useful ?

A

A trait bound is a constraint on a generic type, it allows the compile enforce the desired behaviour we want in our code. By ensuring the type has that behaviour.

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

What are lifetimes in Rust ?

A

Lifetimes are generics that ensure a reference in our code is valid for as long as we need them to be.

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

What are Rust’s elision rules ?

A

They are patterns which have been baked into Rust’s reference analyser. Overtime the rust team noticed recurring patterns, on how rust programmers wrote lifetime related code. This led them to build those patterns into the compiler so programmers didn’t have to be explicit in those cases.

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

What are 3 rules the Rust compiler uses to figure lifetimes without explicit annotations ?

A
  1. Each method parameter gets a different lifetime
  2. Methods with a single input lifetime parameter, get that lifetime attached to all output lifetime parameters
  3. If a method with multiple parameters has self or mut self, the lifetime of self is attached to all output parameters
17
Q

What is the static lifetime ?

A

The static lifetime indicates that the affected reference should live for the entire duration of the program.

18
Q

What is a pointer and how does it work in Rust ?

A

A pointer is a variable that stores some address in memory. This address points to some data stored on the heap.

19
Q

What is a smart pointer and how does the Defer and Drop traits influence its behaviour ?

A

A smart pointer is a datastructure that has the functions of a pointer with metadata and extra capabilities.

The Deref trait allows an instance of a smart pointer behave like a reference ie be deferenced like a regular pointer using (*), while the Drop trait allows the programmer write code that should be run when an instance of the smart pointer goes out of scope

20
Q

Smart pointers to note

A
  • Box<T> : used to allocate data on the heap</T>
  • Rc<T> : reference counting smart pointer, keeps a count of all owners to the its data</T>
  • Ref<T> and RefMut<T> accessed via Ref<Cell> : used to enforce ownership rules at runtime</Cell></T></T>
21
Q

What are some use cases for the Box<T> smart pointer

A
  1. You have a type whose size can’t be known at compile time, but you want to use that value in a context that requires that its exact size be known
  2. You want to transfer ownership of a large piece of data without having it copied
  3. You want to use a value of a type but only care that it implements a particular trait, rather than being a specific type
22
Q

What is Derefcoercion ?

A

Its a feature of the rust compiler that converts a reference of a type that implements the Deref trait into a reference of another.

23
Q

Under what conditions does rust do a Deref coercion ?

A
  1. From a &T to &U (an immutable ref of T to an immutable ref of U)
  2. mut &T to a mut &U (a mut ref of T to a mut ref of U)
  3. mut &T to &U (a mut ref of T to an immutable ref of U)
24
Q

Why isn’t it possible to do a Deref coercion from an immutable reference to a mutable one

A

Because doing so would violate the reference borrowing rules. Rust cannot guarantee that more than one immutable reference exists for the referenced data.

25
Q

What is interior mutability ?

A

Its a design pattern in rust that allows you to mutate data even when there are immutable references to that data. It does that using unsafe code. Unsafe here means that rather than letting rust enforce the borrowing rules, we will like to do so manually.

26
Q

What is the Cow smart pointer and how does it function ?

A

The Cow clone on write smart pointer is a smart pointer that provides immutable access to a borrowed data, and lazily clones it if mutability or ownership is required.

27
Q

What are the kinds of macros in Rust ?

A

There are 2 kinds of macros, they are;
* Declarative
* Procedural

Procedural macros are of 3 kinds;
* Custom #derive[] like macros
* Attribute-like macros
* Function-like macros

28
Q
A