Rust Language Flashcards
What are the rust ownership rules ?
- 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.
What is the function of the memory allocator in Rust ?
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.
Can you explain the drop
function and how it works ?
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.
Can you explain the difference between variable move
and copy
in Rust ?
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 does Rust know when to do a move
or a copy
?
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.
Why does moving a reference to a String (&String) result in copying fewer bytes than moving a String ?
Because the String type consists of a pointer, a length and a capacity, while the &String consists of the pointer alone.
What are two reference rules when dealing with Rust ?
- At any given time you can have one mutable reference or any number of immutable references
- References must be valid
What are associated functions in Rust and what are the two types ?
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
In what case would you move a reference to self
in an implementation method, as opposed to borrowing it.
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.
What are Generic Types
?
They are abstract stand in types, for concrete types or properties. They allow a programmer write, functions that can be used with any type.
Is there a performance penalty with using generics ? If not how does rust accomplish this ?
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.
What is the use case for Generics ?
Generics help reduce code duplication by allowing the programmer write code, that can be used for any type T
What are Trait bounds
and why are they useful ?
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.
What are lifetimes in Rust ?
Lifetimes are generics that ensure a reference in our code is valid for as long as we need them to be.
What are Rust’s elision rules ?
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.