Rust keywords Flashcards
For revising rust's keyword
What does the loop keyword do in Rust?
loop creates an infinite loop. You can exit it using break
How do you create a mutable variable in Rust?
Use the mut keyword. Example: let mut x = 10;
What is the match statement used for in Rust?
match is used for pattern matching, similar to switch-case in other languages.
What is the difference between while and for loops in Rust?
while loops run as long as a condition is true. for loops iterate over collections or ranges.
What does the &str type represent in function parameters?
&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.
What is the purpose of the break keyword in a loop?
break is used to exit a loop early, stopping its execution.
How do you skip the current iteration of a loop and move to the next one?
Use the continue keyword
How do you declare a constant value in Rust?
using const
example:
const MAX_POINTS: u32 = 100;
What is the difference between String and &str in Rust?
String is a heap-allocated, growable string. &str is a reference to a string slice, typically a view into a String.
How do you convert a string slice (&str) to a String?
Use the to_string method. Example: “Hello”.to_string();
How do you create an infinite loop in Rust?
loop {
// Code here
}