Syntax Quiz - Languages Rust
Free practice quiz: Syntax (Languages Rust). 52 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
Play all 52 questions freeSample questions
In Rust, what keyword is used to declare a mutable variable?
- let
- mut
- var
- const
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?
- let
- const
- static
- var
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?
- A function that can return multiple values.
- A collection of related data, similar to a class.
- A type that can represent a single value from a set of possible values.
- A reference to another data structure.
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?
- To define a new trait.
- To define a type alias.
- To implement methods for a struct or enum, or to implement a trait for a type.
- To create a generic function.
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?
- Storing ordered sequences of elements.
- Storing key-value pairs where keys are unique.
- Storing fixed-size arrays of elements.
- Storing boolean flags.
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.