Intermediate Rust test (Intermediate)
30 questions · 35 minutes · Free
Lifetimes, traits, error handling and collections.
Take the Intermediate Rust test
What it covers
- Lifetimes
- Traits and generics
- Result and error handling
- Iterators and closures
- Smart pointers
Interview format
- Questions: 30 questions — 18 multiple choice, 9 multi-answer, 3 predict-the-output
- Time: 35 minutes, timed from the first question
- Difficulty: Intermediate
- Skill area: Backend
- Price: Free
Sample questions
5 sample questions from the Intermediate Rust test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
What is the primary purpose of the `Deref` trait in Rust?
- The `Deref` trait is used to define custom memory management for heap-allocated data types.
- The `Deref` trait allows smart pointers to be used like references, enabling automatic dereferencing when needed.
- The `Deref` trait is used exclusively for implementing drop behavior for smart pointers.
- The `Deref` trait is used to enforce compile-time borrowing rules in Rust.
Answer: B. The `Deref` trait allows smart pointers to be used like references, enabling automatic dereferencing when needed.
The `Deref` trait enables smart pointers to behave like references by providing a `deref` method that returns a reference to the underlying data. This allows for seamless usage of smart pointers in contexts that expect references, such as function arguments or assignments. The other options are either incorrect or not directly related to the purpose of the `Deref` trait.
What is the primary purpose of the ? operator in Rust when working with Result types?
- The ? operator is used to handle panic! in Rust and provide a fallback value.
- The ? operator is used to unwrap Option types and return the value inside them.
- The ? operator returns the error value from the current function, allowing it to propagate upwards.
- The ? operator is used to call functions and return their results directly.
Answer: C. The ? operator returns the error value from the current function, allowing it to propagate upwards.
The ? operator is used in Rust for error propagation. When used inside a function that returns a Result, it unwraps the Result and returns the value if it is Ok, or returns the error if it is Err, allowing the error to propagate up the call stack. This makes code more concise and readable by avoiding nested match statements. The other options are either incorrect or describe different concepts.
Which statement accurately describes a requirement for using trait objects with dynamic dispatch in Rust?
- Trait objects can be directly assigned to variables without any additional syntax.
- Dynamic dispatch with trait objects does not require any explicit lifetime parameters.
- Trait objects can only be used with static lifetimes.
- The trait object must be stored in a box or another smart pointer.
Answer: D. The trait object must be stored in a box or another smart pointer.
In Rust, when using trait objects with dynamic dispatch, the trait object must be stored in a smart pointer like Box, Rc, or Arc because trait objects have an unknown size at compile time. This allows the compiler to manage the memory safely and dynamically. The other options are either incorrect or do not address the core issue of handling trait objects with dynamic dispatch.
Which of the following statements correctly describes the behavior of the `map_or` combinator in Rust?
- The `map_or` combinator transforms the `Err` value of a `Result` and returns a default value if the `Result` is `Ok`.
- The `map_or` combinator is used to chain multiple `Result` types together using the `and_then` method.
- The `map_or` combinator is used to propagate errors in a `Result` type using the `?` operator.
- The `map_or` combinator applies a function to the `Ok` value of a `Result` and returns a default value if the `Result` is `Err`.
Answer: D. The `map_or` combinator applies a function to the `Ok` value of a `Result` and returns a default value if the `Result` is `Err`.
The `map_or` combinator is used to transform the `Ok` value of a `Result` and provide a default value in case of an `Err`. This is different from `map`, which only transforms the `Ok` value and leaves the `Err` unchanged. `map_or` is particularly useful for handling `Result` types in a concise way when a fallback value is needed.
Consider the following code snippet in Rust: ```rust let s = "hello"; let result = s.map(|s| s.len()); ``` What will happen when this code is compiled and executed?
- The code will compile but will panic at runtime.
- The code will not compile because the `map` method is not called on a `Result` type.
- The code will compile and return the length of the string.
- The code will compile and return `None`.
Answer: B. The code will not compile because the `map` method is not called on a `Result` type.
The `map` method is defined on the `Result` type and is used for transforming the value inside a `Result` when it is `Ok`. In the given code, the `map` method is being called on a `String` instead of a `Result`, which is the incorrect usage. The correct approach would be to call `map` on a `Result` type, such as `result.map(|s| s.len())`.
Frequently asked questions
- How many questions are in the Intermediate Rust test?
- 30 questions (18 multiple choice, 9 multi-answer, 3 predict-the-output), with 35 minutes to answer them.
- What does the Intermediate Rust test cover?
- Lifetimes, Traits and generics, Result and error handling, Iterators and closures, Smart pointers.
- Is the Intermediate Rust test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Intermediate Rust test?
- Intermediate, in Backend.