Basic Rust Interview Question Flashcards

1
Q

What is Rust?

A

Rust is a general-purpose memory-safe high-performance systems programming language.

It enables developers to write correct and maintainable code.

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

Why does Rust have high performance?

A

Rust has high performance because it compiles directly to native machine code.

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

Why do Rust programs consume a small amount of memory?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you use cargo to build and test Rust code?

A

To use cargo to build Rust code, the build command gets used:

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

What kinds of programs can you write with Rust?

A

Using Rust, you can create web servers, command line programs, databases, audio plugins, text processors, operating systems, device drivers, and more.

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

What is the difference between a Rust enum and struct?

A

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.

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

Provide an example of an impl block in Rust

A

struct Number(i32);

impl Number {
pub fn new(n: i32) -> Self {
Self(n)
}
}

let five = Number::new(5);

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

How do you create an infinite loop in Rust?

A

loop {
// …
}

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

How can you mutate variables in Rust?

A

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:

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

What happens to borrowed data at the end of a Rust function?

A

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.

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

What happens to owned data at the end of a Rust function?

A

When writing a function that takes ownership of data, the data gets dropped (deleted) at the end of a function.

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

What does #[derive(Debug)] do in Rust?

A

Using #[derive(Debug)] allows a struct or enum to get printed with the debug formatting token {:?} in the println! and format! macros.

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

What’s the difference between .unwrap() and .expect() in Rust?

A

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().

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

Why is the return keyword in Rust optional? Provide examples

A

The return keyword is optional in Rust because Rust is an expression-based language.

fn one() -> u32 {
1
}

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

What is an example of a match expression in Rust?

A

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”),
}

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

Can you assign the result of a Rust match expression to a variable binding?

A

Yes. Since match is an expression, assigning the result gets accomplished like this:

let t = true;
let one = match t {
true => 1,
false => 0,
};

17
Q

What happens if you add a new variant to a Rust enum without changing any other code?

A

Adding a new variant to an enum without changing any other code may trigger compiler errors elsewhere in the program.

18
Q

What Rust keyword will iterate through a collection?

A

Using the for will iterate through a collection:

let nums = vec![1, 2, 3];
for n in nums {
println!(“{n}”)
}

19
Q

. What Rust code would you write for printing information to the terminal?

A

println!(“hello world”);

20
Q

hat’s a Rust Vec and when would you use it?

A

A Vec is a linear collection of elements, with similarities to a dynamic array present in other languages.

You would use a Vec when you need to store elements in a defined order, or when you plan on iterating over the elements.