Syntax Quiz - Languages Rust
Free practice quiz: Syntax (Languages Rust). 52 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages Rust practice quiz has 52 multiple-choice questions on Syntax, and 52 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 52 questions, freeSample questions
In Rust, what keyword is used to declare a mutable variable?
Show answer
Answer: B. mut
Variables in Rust are immutable by default. To make a variable mutable, you must prefix the `let` binding with the `mut` keyword. For example: `let mut x = 5;`.
Which keyword is used to define a constant in Rust?
Show answer
Answer: B. const
Constants in Rust are declared using the `const` keyword. They must be annotated with their type and have a value that can be determined at compile time. They are always immutable.
What does the `struct` keyword define in Rust?
Show answer
Answer: B. A collection of related data, similar to a class.
`struct` is used to define custom data types that group together multiple related values. These are analogous to structures in C or classes in other object-oriented languages.
What is the purpose of the `impl` keyword in Rust?
Show answer
Answer: C. To implement methods for a struct or enum, or to implement a trait for a type.
`impl` is used to define implementation blocks, which contain methods associated with a particular type (struct or enum) or to implement a trait for a type.
What is a `HashMap` in Rust used for?
Show answer
Answer: B. Storing key-value pairs where keys are unique.
A `HashMap` allows you to store data in key-value pairs. Each key must be unique, and you can use the key to retrieve its associated value.
More questions in this quiz
Answers and explanations are in the full quiz.
- What is the correct syntax for a string literal in Rust?
- Which of the following is the correct way to define a function in Rust?
- What does the `println!` macro do in Rust?
- In Rust, what is the primary difference between `let` and `let mut`?
- Which symbol denotes a comment in Rust?
- What is the correct way to define an integer variable with a specific type, like a 32-bit signed integer, in Rust?
- What is the purpose of the `return` keyword in a Rust function?
- Which of the following represents a boolean literal in Rust?
- What is the correct syntax for creating a tuple in Rust?
- What is the syntax for accessing an element in a tuple in Rust?
- Which of the following is NOT a primitive data type in Rust?
- What is the purpose of the `main` function in a Rust program?
- Which symbol is used to dereference a pointer or reference in Rust?
- What is the correct syntax for an array literal in Rust?
- How do you access an element in a Rust array?
- What does the `&` symbol signify in Rust?
- Which keyword is used to define a mutable reference in Rust?
- What is the purpose of the `::` operator in Rust?
- Which of the following is the correct way to define an enum in Rust?
- What is the syntax for pattern matching in Rust?
- In Rust, what is the purpose of the `loop` keyword?
- What does `break` do within a `loop` or `while` in Rust?
- Which keyword is used to continue to the next iteration of a loop in Rust?
- What is the syntax for a `while` loop in Rust?
- What is the syntax for a `for` loop that iterates over a range in Rust?
- What is the purpose of the `move` keyword in Rust?
- What is the syntax for a closure in Rust?
- Which of the following correctly defines a mutable closure that takes no arguments and returns nothing?
- How do you access the `self` parameter within a method of a struct in Rust?
- What does the `Self` keyword refer to within an `impl` block?
- What is a `trait` in Rust?
- Which keyword is used to define a trait in Rust?
- What is the purpose of the `dyn` keyword?
- What is the syntax for declaring a vector in Rust?
- How do you push an element to a mutable vector in Rust?
- What is the purpose of the `mut` keyword when declaring a variable like `let mut my_vec = Vec::new();`?
- What does the `?` operator do at the end of an expression in Rust?
- How do you typically create a new `HashMap` in Rust?
- What is the `use` keyword primarily used for in Rust?
- What does `mod` keyword define in Rust?
- What is the purpose of `#[derive(...)]` in Rust?
- What is the primary difference between a `struct` and an `enum` in Rust?
- Which of the following signifies a lifetime annotation in Rust?
- What is the purpose of `std::io` in Rust?
- What does the `&mut` lifetime annotation imply?
- What is the correct syntax for an associated function (static method) in Rust?
- Which of the following correctly defines a method that takes an immutable reference to self?