Rust keywords Flashcards

For revising rust's keyword

1
Q

What does the loop keyword do in Rust?

A

loop creates an infinite loop. You can exit it using break

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

How do you create a mutable variable in Rust?

A

Use the mut keyword. Example: let mut x = 10;

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

What is the match statement used for in Rust?

A

match is used for pattern matching, similar to switch-case in other languages.

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

What is the difference between while and for loops in Rust?

A

while loops run as long as a condition is true. for loops iterate over collections or ranges.

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

What does the &str type represent in function parameters?

A

&str is a string slice, which represents a reference to a string. It’s used when you want to pass a string without transferring ownership.

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

What is the purpose of the break keyword in a loop?

A

break is used to exit a loop early, stopping its execution.

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

How do you skip the current iteration of a loop and move to the next one?

A

Use the continue keyword

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

How do you declare a constant value in Rust?

A

using const

example:
const MAX_POINTS: u32 = 100;

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

What is the difference between String and &str in Rust?

A

String is a heap-allocated, growable string. &str is a reference to a string slice, typically a view into a String.

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

How do you convert a string slice (&str) to a String?

A

Use the to_string method. Example: “Hello”.to_string();

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

How do you create an infinite loop in Rust?

A

loop {
// Code here
}

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