What are the rust ownership rules ?
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 ?
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.
What are 3 rules the Rust compiler uses to figure lifetimes without explicit annotations ?
self or mut self, the lifetime of self is attached to all output parametersWhat is the static lifetime ?
The static lifetime indicates that the affected reference should live for the entire duration of the program.
What is a pointer and how does it work in Rust ?
A pointer is a variable that stores some address in memory. This address points to some data stored on the heap.
What is a smart pointer and how does the Defer and Drop traits influence its behaviour ?
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
Smart pointers to note
What are some use cases for the Box<T> smart pointer
What is Derefcoercion ?
Its a feature of the rust compiler that converts a reference of a type that implements the Deref trait into a reference of another.
Under what conditions does rust do a Deref coercion ?
Why isn’t it possible to do a Deref coercion from an immutable reference to a mutable one
Because doing so would violate the reference borrowing rules. Rust cannot guarantee that more than one immutable reference exists for the referenced data.