Advanced Rust interview questions — Part 3

Mayank Choubey
Tech Tonic
Published in
17 min readMar 31, 2024


In this series, we are looking into advanced Rust interview questions, 10 at a time. This is the third part, which covers questions 21 to 30. The other parts are:

Question 21 — Explain the concept of “pinning” in Rust and how it relates to asynchronous programming.

In Rust, asynchronous programming allows our code to handle multiple tasks at once without blocking. This is particularly useful for operations that involve waiting for external events, like network requests or user input. However, asynchronous programming introduces a new challenge: ensuring memory safety when dealing with data used across these tasks.

This is where pinning comes into play. Pinning is a concept in Rust’s asynchronous programming model that ensures the location of data in memory remains fixed while it’s being used by multiple asynchronous tasks. This prevents a potential issue called “data races,” which can occur when multiple tasks try to access or modify the same data concurrently, leading to unpredictable program behavior.

Futures and the need for pinning

In Rust’s asynchronous model, tasks are represented by futures. A future is a value that holds the eventual result of an asynchronous operation. However, some futures, particularly those involving self-referential structs (structs containing references to themselves), can be problematic. Imagine a future that holds a linked list where each node references the next node in the list. If this future is allowed to move around in memory while tasks are iterating over the list, the references within the nodes might become invalid, leading to a data race.

Pinning ensures location stability

To prevent this issue, we can pin the future. Pinning essentially “fixes” the location of the future’s data in memory. This guarantees that the references within the future (like those in our linked list example) remain valid throughout its lifetime, even if the future itself is passed between tasks.

Rust provides the Pin type to achieve this. By wrapping a future with Pin, we inform the compiler that its data must not be moved while it's being used.

The Unpin trait

Not all futures require pinning. The Unpin trait indicates that a future's data can be safely moved. In essence, futures implementing Unpin are guaranteed not to contain self-references or other ownership structures that could be invalidated by movement.

Common futures like those returned by async blocks or functions like std::future::ready are typically Unpin. However, as mentioned earlier, self-referential structs or futures that hold references to borrowed data might not be Unpin.

Using Pin in practice

Here’s a simple example to illustrate pinning:

struct Node {
value: i32,
next: Option<Pin<Box<Node>>>, // Pinned pointer to the next node

impl Node {
async fn traverse(&mut self) {
// Iterate through the linked list using the pinned references
if let Some(ref mut next) = {
println!("Value: {}", self.value);

fn main() {
// ... (create a linked list of nodes)
let mut head = Pin::new(Box::new(head_node));

In this example, the Node struct holds an Option<Pin<Box<Node>>> for the next node in the linked list. We use Pin to ensure the location of the boxed node remains fixed during the asynchronous traversal process.

Benefits of pinning

Pinning plays a crucial role in maintaining memory safety in Rust’s asynchronous programming model. By ensuring data location stability, it prevents data races and potential program crashes. This leads to more reliable and predictable asynchronous code.

Question 22 — What are the differences between Box, Rc, and Arc in Rust, and when would you use each one?

In Rust’s memory management system, we often encounter scenarios where data needs to be shared between different parts of our application. While Rust enforces ownership rules to prevent memory issues, these rules can sometimes be restrictive when dealing with shared data. There are three key tools for managing memory ownership in the context of sharing data: Box, Rc, and Arc.

Box (Heap allocation)

  • Box<T> allocates data on the heap and returns a smart pointer to that data. The Box itself owns the allocated memory, and when the Box goes out of scope, the memory is automatically deallocated.
  • Box is used when we need a single owner for a dynamically allocated piece of data. It's a good choice for returning heap-allocated data from functions or storing data on the heap for later use.
fn create_box() -> Box<i32> {
let value = 5;
Box::new(value) // Allocate memory and return a Box

fn main() {
let boxed_value = create_box();
println!("Value: {}", *boxed_value); // Dereference the Box to access the value

Rc (Reference counting)

  • Rc<T> (reference counting) allows multiple owners to share a single piece of data on the heap. It keeps a reference count of how many owners there are for the underlying data. When the last Rc goes out of scope, the memory is deallocated.
  • Rc is used when we need to share ownership of a piece of data between multiple parts of our application that don't necessarily have a parent-child relationship.
use std::rc::Rc;

struct Node {
value: i32,
next: Option<Rc<Node>>, // Rc for shared ownership of next node

fn main() {
let node1 = Rc::new(Node { value: 1, next: None });
let node2 = Rc::clone(&node1); // Clone the Rc to create another owner = Some(Rc::clone(&node2));
// Both node1 and node2 now point to the same data on the heap

Arc (Atomic Rc — thread safety)

  • Arc<T> (atomic reference counting) is similar to Rc but provides thread safety. It uses atomic operations to manage the reference count, allowing the data to be shared across multiple threads.
  • Arc is used when we need to share ownership of a piece of data between multiple threads concurrently. It ensures safe access and avoids data races.
use std::sync::Arc;
use std::thread;

fn main() {
let shared_data = Arc::new(5);
let thread1 = thread::spawn(|| {
println!("Thread 1: {}", *shared_data);
let thread2 = thread::spawn(|| {
println!("Thread 2: {}", *shared_data);

Question 23 — Describe the Send and Sync traits in Rust and explain their significance in concurrent programming.

Significance of Send and Sync

Send and Sync are fundamental for building robust concurrent programs in Rust. They enforce thread safety by ensuring types are used appropriately in multithreaded contexts. The compiler enforces these traits, preventing code that might lead to data races or other concurrency issues. This helps us write safer and more predictable concurrent code.

Send trait

The Send trait signifies that a type can be safely transferred between threads without violating ownership rules or causing undefined behavior. It guarantees that the type's data can be moved from one thread to another without any issues. The Send trait is primarily used when we need to pass ownership of data between threads. This includes scenarios like moving data to a worker thread for processing or sending data across threads using channels.

fn send_data_to_thread<T: Send>(data: T, thread_fn: fn(T)) {
// ... (spawn a thread and move data using channels or other mechanisms)

In this example, the send_data_to_thread function requires the data type T to implement Send. This ensures safe ownership transfer to the spawned thread.

Sync trait

The Sync trait indicates that a type can be safely shared and accessed immutably by multiple threads concurrently. It essentially guarantees that the type's data is thread-safe for read access, even if multiple threads are trying to access it simultaneously. The Sync trait is used when we need to share data between threads for read-only access. This includes scenarios like sharing static data structures or accessing global configuration from multiple threads.

use std::sync::Mutex; // Mutex for thread-safe access

struct SharedCounter {
value: i32,

static mut COUNTER: Mutex<SharedCounter> = Mutex::new(SharedCounter { value: 0 });

fn increment_counter() {
let mut counter = unsafe { COUNTER.lock().unwrap() }; // Acquire lock for mutable access
counter.value += 1;

Here, the SharedCounter struct isn't marked as Sync because it requires a mutex for safe mutable access. However, if we only needed read access from multiple threads, marking it Sync would be appropriate.

Relationship between Send and Sync

An important relationship exists between them:

Any type that is Sync is also implicitly Send. This means if data is safe to share immutably between threads, it can also be safely transferred between threads by ownership.


It’s important to note that Send and Sync don't guarantee immutability. They only ensure safe access based on the defined trait (ownership transfer for Send and immutable access for Sync). For mutable access in a concurrent environment, mechanisms like mutexes or other synchronization primitives are necessary.

Question 24 — What are the benefits and drawbacks of using Rust’s ownership model compared to garbage-collected languages like Java, Python, or Go?

Rust’s ownership model stands out as a unique approach compared to garbage-collected languages like Java, Python, or Go. Like everything else, there are some advantages and disadvantages of Rust’s ownership system when compared to garbage collection.

Benefits of Rust’s ownership model

Memory Safety

A core strength of Rust’s ownership system is its ability to guarantee memory safety at compile time. By enforcing ownership rules that dictate how data is created, used, and destroyed, Rust eliminates the possibility of memory leaks (dangling pointers) and double frees that can plague garbage-collected languages.


The absence of a garbage collector in Rust translates to improved performance. Without the overhead of automatic memory management, Rust applications generally have faster execution speeds and lower memory consumption compared to garbage-collected languages, especially for memory-intensive tasks.

Fine-grained control

Rust’s ownership system empowers us with detailed control over memory management. We explicitly decide how data is owned and passed around, leading to efficient memory usage and avoiding unnecessary allocations or copies. This fine-grained control can be particularly beneficial for performance-critical applications.

Immutability by default

Rust promotes immutability by default, which simplifies reasoning about program state and reduces the potential for data races in concurrent applications.

Drawbacks of Rust’s ownership model
Nothing is perfect in this world. While Rust’s ownership model has many advantages, there are some disadvantages too:

Steeper learning curve

Compared to garbage-collected languages, Rust’s ownership system introduces a steeper learning curve. Understanding ownership rules and borrowing mechanisms can require more upfront effort from developers. However, this investment often pays off in the long run with more reliable and performant code.

Error handling

While Rust enforces memory safety, dealing with ownership-related errors can sometimes be more verbose compared to garbage-collected languages. Error messages might require a deeper understanding of ownership rules to fix.

Less convenient for rapid prototyping

The focus on explicit memory management in Rust can be less convenient for rapid prototyping or scripting tasks where memory leaks might not be a critical concern. Garbage-collected languages offer a simpler approach for quick experimentation.

Question 25 — Explain the differences between HashMap, BTreeMap, and HashSet in Rust, and when would you choose one over the others?

In Rust’s rich collection of data structures, HashMap, BTreeMap, and HashSet provide powerful tools for storing and retrieving data efficiently. Let’s take a look at each of them.


HashMap<K, V> is an unordered hash table that stores key-value pairs. It uses a hashing function to map keys to bucket indices, enabling fast average-case lookups, insertions, and removals based on the key. We can use HashMap when we need a fast and efficient way to store and retrieve data by a unique key, and the order of elements is not important. This makes it ideal for scenarios like caching, configuration files, or implementing symbol tables.

use std::collections::HashMap;

fn main() {
let mut user_data: HashMap<u32, String> = HashMap::new();
user_data.insert(1, "Alice".to_string());
user_data.insert(2, "Bob".to_string());
let username = user_data.get(&1);
if let Some(name) = username {
println!("User ID 1: {}", name);


BTreeMap<K, V> is a sorted map that stores key-value pairs in a binary search tree structure. It guarantees elements are ordered based on the key’s natural ordering or a custom comparator. This enables efficient retrieval and iteration in sorted order. BTreeMap is useful when we need to access or iterate over elements in a specific order determined by the key. This is useful for maintaining sorted lists, implementing leaderboards, or processing data in a particular sequence.

use std::collections::BTreeMap;

fn main() {
let mut word_counts: BTreeMap<String, u32> = BTreeMap::new();
word_counts.insert("hello".to_string(), 2);
word_counts.insert("world".to_string(), 1);
for (word, count) in &word_counts {
println!("Word: {} - Count: {}", word, count);


HashSet<T> is an unordered collection that stores unique elements of type T. It uses a hashing function to efficiently check for membership and prevent duplicate elements. We use HashSet when we need to efficiently check if a particular element exists in a collection or eliminate duplicates. This is useful for implementing sets of unique identifiers, deduplicating data streams, or performing set operations like intersection or union.

use std::collections::HashSet;

fn main() {
let mut unique_numbers: HashSet<i32> = HashSet::new();
unique_numbers.insert(1); // Duplicate will not be inserted
if unique_numbers.contains(&3) {
println!("Number 3 not found");

Which one to choose when?

The choice between HashMap, BTreeMap, and HashSet depends on our specific needs:

  • If fast key-based access is essential, and order doesn’t matter, we can use HashMap
  • If sorted access or iteration is required, we can use BTreeMap
  • If we need to check for unique elements or perform set operations, we should use HashSet

Question 26 — How does Rust’s trait system enable ad-hoc polymorphism, and what are the benefits of this approach compared to inheritance-based polymorphism in other languages?

In object-oriented programming, polymorphism is a fundamental concept that allows objects of different types to respond to the same method call. Rust, however, takes a unique approach to polymorphism through its trait system.

Ad-hoc polymorphism with traits

In Rust, traits are collections of function prototypes that define a specific behavior. Types can implement multiple traits, indicating they can exhibit the behavior defined by those traits. This enables a high degree of flexibility in how types can be used polymorphically. We can define functions that accept references to any type that implements a particular trait. This allows the function to work with different concrete types as long as they provide the required behavior defined by the trait.

trait Shape {
fn area(&self) -> f64;

struct Square {
side_length: f64,

impl Shape for Square {
fn area(&self) -> f64 {
self.side_length * self.side_length

struct Circle {
radius: f64,

impl Shape for Circle {
fn area(&self) -> f64 {
3.14159 * self.radius * self.radius

fn calculate_area(shape: &dyn Shape) -> f64 {

fn main() {
let square = Square { side_length: 5.0 };
let circle = Circle { radius: 2.0 };
println!("Square area: {}", calculate_area(&square));
println!("Circle area: {}", calculate_area(&circle));

In the above example, the Shape trait defines the area method. Both Square and Circle implement Shape, allowing them to be used with the calculate_area function, which operates on any type implementing Shape.

Benefits of trait-based polymorphism

Compared to inheritance-based polymorphism in other languages, Rust’s trait system offers several advantages:

  • Flexibility: Traits enable ad-hoc polymorphism, meaning types can implement any combination of traits, not limited by a strict inheritance hierarchy.
  • Composability: Traits can be combined to create more complex behaviors. Types can implement multiple traits, allowing them to participate in various functionalities.
  • No diamond problem: Inheritance hierarchies can lead to the “diamond problem” where multiple inheritance creates ambiguity. Traits avoid this by promoting composition over inheritance.
  • Static typing: Rust’s type system ensures type safety at compile time. Traits are checked for compatibility during compilation, preventing runtime errors associated with dynamic dispatch used in inheritance-based polymorphism.

Question 27 — Explain the role of the std::mem module in Rust and its functions such as size_of, align_of, and forget

In the world of Rust’s memory management, the std::mem module provides a set of essential functions for manipulating and querying memory. The std::mem module offers functions for performing low-level memory operations and obtaining information about types in memory. These functions are primarily used for advanced memory management scenarios or for interfacing with unsafe code.

Let’s explore some of the key functions, including size_of, align_of, and forget.


This function returns the size (in bytes) that a value of type T occupies in memory. This information can be useful for understanding memory usage patterns or optimizing data structures.

use std::mem;

fn main() {
let x: i32 = 42;
let size_of_x = mem::size_of::<i32>();
println!("Size of i32: {} bytes", size_of_x);


This function returns the minimum alignment requirement (in bytes) for a value of type T. Alignment refers to the memory address boundaries on which a type can be efficiently stored and accessed by the CPU. This information is important for optimizing memory layout and performance.

fn main() {
let x: i32 = 42;
let alignment_of_x = mem::align_of::<i32>();
println!("Alignment of i32: {} bytes", alignment_of_x);

forget<T>(value: T)

This function, marked as unsafe, informs the compiler that it should no longer track the ownership of the value passed to it. This essentially "forgets" about the value, allowing potential memory leaks if not used cautiously. It's typically used in advanced scenarios like raw pointer manipulation or implementing custom allocators.

unsafe fn forget_value<T>(value: T) {
mem::forget(value); // Potentially leaks memory if not handled properly

Important considerations

  • It’s essential to exercise caution when using unsafe functions like forget. Improper usage can lead to memory leaks, undefined behavior, or other safety issues.
  • In most cases, Rust’s ownership system provides a safe and efficient way to manage memory. The functions in std::mem are primarily for advanced use cases or for interfacing with unsafe code.

Question 28 — Explain the concept of “type erasure” and its implications for trait objects and dynamic dispatch in Rust

Type erasure refers to the process of removing the specific type information from a value at compile time while preserving the implemented trait information. This essentially creates a generic representation that can hold values of different concrete types as long as they implement the same trait.

Trait objects and dynamic dispatch

Trait objects, denoted by dyn Trait, are a mechanism to achieve dynamic dispatch in Rust. They represent an erased type that implements a specific trait. When a function accepts a trait object as a parameter, the compiler doesn't know the exact concrete type at compile time.

trait Printable {
fn print(&self);

struct Number(i32);

impl Printable for Number {
fn print(&self) {
println!("Number: {}", self.0);

struct Text(String);

impl Printable for Text {
fn print(&self) {
println!("Text: {}", self.0);

fn print_anything(value: &dyn Printable) {
value.print(); // Dynamic dispatch based on the actual type at runtime

fn main() {
let number = Number(42);
let text = Text("Hello, world!".to_string());

In this example, the print_anything function accepts a reference to a trait object &dyn Printable. The compiler doesn’t know if it’s a Number or a Text at compile time. However, because both types implement Printable, the appropriate print method is called at runtime based on the actual concrete type of the value being passed. This is dynamic dispatch in action.

Implications of type erasure

Type erasure provides flexibility in function design, allowing them to work with different types that implement a common trait. However, it comes with some trade-offs:

  • Performance overhead: Dynamic dispatch can incur a slight performance overhead compared to statically dispatched function calls where the type is known at compile time.
  • Limited Functionality: Trait objects cannot access methods that are not defined in the trait itself. They lose access to type-specific methods available on concrete types.

Question 29 — Discuss Rust’s approach to memory layout optimization, including techniques such as struct layout packing and alignment

Rust’s memory layout optimization techniques, including packing and alignment, empower us to create compact and performant data structures. By default, Rust adheres to a predictable memory layout for structs. Fields are placed in memory contiguously, following their order of declaration within the struct definition. This layout prioritizes clarity and ease of reasoning about memory access.

In some scenarios, we can achieve a more space-efficient layout by using the #[repr(packed)] attribute on a struct. This instructs the compiler to pack struct fields together tightly, potentially introducing padding between them. Padding bytes are inserted to ensure proper alignment for certain data types.

struct PackedData {
header: u8,
data: u16, // Padding might be inserted here
flag: bool,

Packing can be beneficial for small structs that frequently participate in low-level operations or for interfacing with foreign code that expects a specific memory layout. However, it’s essential to use packing judiciously, as it can:

  • Reduce readability: Packed structs can be less readable due to potential padding and non-intuitive memory offsets for fields.
  • Portability issues: Packing behavior can vary across different compiler implementations or target architectures.

Alignment refers to the memory address boundary on which a type can be efficiently stored and accessed by the CPU. Rust ensures proper alignment for all types by default. However, we can sometimes control alignment using the #[repr(align(N))] attribute on a struct or a field within the struct. This specifies the minimum alignment requirement (in bytes) for the data.

struct LargeData {
value: u64,
// Padding might be inserted here to ensure 64-byte alignment

Enforcing alignment can be necessary when:

  • Interfacing with hardware or external libraries that have specific alignment requirements.
  • Optimizing memory access patterns for performance-critical data structures.

The decision to use packing or alignment attributes depends on specific requirements:

  • If space efficiency is a top priority and readability is less of a concern, packing might be suitable.
  • If alignment is crucial for performance or hardware compatibility, alignment attributes can be employed.
  • In most cases, the default memory layout with automatic alignment is sufficient for clear and efficient data structures.

Question 30 — How does Rust handle cyclic references and memory leaks, especially in scenarios involving reference counting and shared ownership?

Rust’s ownership system is renowned for its memory safety guarantees. However, cyclic references can create challenges, potentially leading to memory leaks if not handled correctly.

A cyclic reference occurs when two or more data structures hold references to each other, creating a circular dependency. This prevents either data structure from being dropped (deallocated) because each reference keeps the other “alive” in memory.

Rust’s Rc<T> (reference counting) and Arc<T> (atomic reference counting) offer ways to share ownership of data between multiple entities. However, in cyclic references involving these types, the reference counts never reach zero, leading to a memory leak.

Here is a common scenario of two Rc and Arc, holding references to each other:

use std::rc::Rc;

struct Node {
data: i32,
next: Option<Rc<Node>>,

fn main() {
let mut node1 = Rc::new(Node { data: 10, next: None });
let node2 = Rc::new(Node { data: 20, next: Some(node1.clone()) }); = Some(node2.clone()); // Cyclic reference created

Here, node1 and node2 hold Rc to each other, preventing deallocation.

The following is another example of Rc<T> or Arc<T> holding a reference to a struct containing an Rc<T> or Arc<T>:

use std::rc::Rc;

struct Graph {
nodes: Vec<Rc<Node>>,

struct Node {
data: i32,
parent: Option<Rc<Graph>>,

fn main() {
let mut graph = Rc::new(Graph { nodes: Vec::new() });
let node = Rc::new(Node { data: 10, parent: Some(graph.clone()) });
graph.nodes.push(node.clone()); // Cyclic reference created

In this example, graph holds an Rc<Node>, and node holds an Rc<Graph>, creating a cycle.

Solutions for shared ownership

Weak<T> (Weak References)

Rust provides Weak<T> alongside Rc<T> and Arc<T>. A Weak<T> can be used to check if the referenced data is still valid without affecting the reference count. If a Weak<T> is converted to Rc<T> or Arc<T>, it attempts to upgrade the weak reference to a strong reference, but only if the data still exists.

use std::rc::{Rc, Weak};

struct Node {
data: i32,
next: Option<Weak<Node>>,

fn main() {
let mut node1 = Rc::new(Node { data: 10, next: None });
let weak_node1 = Rc::downgrade(&node1);
let node2 = Rc::new(Node { data: 20, next: Some(weak_node1.clone()) }); // Weak reference
if let Some(strong_node1) = weak_node1.upgrade() { = Some(Rc::new(Node { data: 30, next: None })); // Update only if strong ref exists

In this example, node2 holds a Weak<Node> to node1. If node1 is deallocated elsewhere, the Weak<T> becomes unusable.

Breaking the cycle with ownership

In some cases, we can restructure our data to avoid cyclic references altogether. Ownership rules in Rust can sometimes be used to break the cycle by ensuring one side of the reference holds sole ownership while the other has access through borrowing.

struct Node {
data: i32,
children: Vec<&Node>,

fn main() {
let mut node1 = Node { data: 10, children: Vec::new() };
let node2 = Node { data: 20, children: Vec::new() };
node1.children.push(&node2); // Node1 borrows Node2 (no cycle)
let mut node3 = Node { data: 30, children: vec![&node1] }; // Node3 borrows Node1 (no cycle)

In this example, Node stores references (&) to its children, preventing cycles.

That’s all about the third part. The other parts are:

Thanks for reading! I hope this would have helped you in some way.