Basic Rust Interview Question Flashcards
What is Rust?
Rust is a general-purpose memory-safe high-performance systems programming language.
It enables developers to write correct and maintainable code.
Why does Rust have high performance?
Rust has high performance because it compiles directly to native machine code.
Why do Rust programs consume a small amount of memory?
Rust allocates the minimum amount of memory required for an operation and only does so when needed. Once the operation finishes, the memory is then deallocated.
How can you use cargo to build and test Rust code?
To use cargo to build Rust code, the build command gets used:
What kinds of programs can you write with Rust?
Using Rust, you can create web servers, command line programs, databases, audio plugins, text processors, operating systems, device drivers, and more.
What is the difference between a Rust enum and struct?
While both enum and struct provide ways to encapsulate data, they do so in different ways:
A struct contains fields and every field in the struct is present at all times. This makes struct appropriate when you need to group data together and have access to all components of that data.
An enum contains variants in which a single variant gets represented at a time.
Provide an example of an impl block in Rust
struct Number(i32);
impl Number {
pub fn new(n: i32) -> Self {
Self(n)
}
}
let five = Number::new(5);
How do you create an infinite loop in Rust?
loop {
// …
}
How can you mutate variables in Rust?
By default, all data in Rust is immutable and cannot get changed without being marked as mutable.
Using the mut keyword changes this behavior and allows changing (mutating) the data:
What happens to borrowed data at the end of a Rust function?
When writing a function that borrows data, the borrowed data will remain available for use after the function ends.
This is because ownership of the data does not transfer when borrowed.
What happens to owned data at the end of a Rust function?
When writing a function that takes ownership of data, the data gets dropped (deleted) at the end of a function.
What does #[derive(Debug)] do in Rust?
Using #[derive(Debug)] allows a struct or enum to get printed with the debug formatting token {:?} in the println! and format! macros.
What’s the difference between .unwrap() and .expect() in Rust?
Both .unwrap() and .expect() will trigger a panic if they execute.
.unwrap() triggers a thread panic and then displays the line number containing the call to .unwrap().
.expect() triggers a thread panic with a customized message, and then displays the line number containing the call to .expect().
Why is the return keyword in Rust optional? Provide examples
The return keyword is optional in Rust because Rust is an expression-based language.
fn one() -> u32 {
1
}
What is an example of a match expression in Rust?
This Rust match expression matches an Option.
When the Option contains Some, the data gets printed to the terminal. When the Option contains None, the message there is no number gets printed to the terminal.
let foo = Some(1);
match foo {
Some(n) => println!(“number is {n}”),
None => println!(“there is no number”),
}