Any2Quiz
Computer Science / Languages / Rust

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 free

Sample questions

  1. In Rust, what keyword is used to declare a mutable variable?

    1. let
    2. mut
    3. var
    4. 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;`.

  2. Which keyword is used to define a constant in Rust?

    1. let
    2. const
    3. static
    4. 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.

  3. What does the `struct` keyword define in Rust?

    1. A function that can return multiple values.
    2. A collection of related data, similar to a class.
    3. A type that can represent a single value from a set of possible values.
    4. 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.

  4. What is the purpose of the `impl` keyword in Rust?

    1. To define a new trait.
    2. To define a type alias.
    3. To implement methods for a struct or enum, or to implement a trait for a type.
    4. 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.

  5. What is a `HashMap` in Rust used for?

    1. Storing ordered sequences of elements.
    2. Storing key-value pairs where keys are unique.
    3. Storing fixed-size arrays of elements.
    4. 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.

Take the full quiz

More quizzes

Browse all Rust quizzes