Chapter 23

Rust Fundamentals Quiz

You can look things up here, just as you would while writing code.

These questions revisit ownership and borrowing, Option and Result, lifetimes, traits, enums, and modules, then ask you to predict behavior in the CSV and environment-file projects. The Rc question includes the shared-ownership rules you need; you don't need to have finished the smart-pointer chapter. Pick an answer to lock it in and reveal the explanation for every choice, not just the right one. The explanations for the other answers are worth reading too, especially when you were choosing between two. Use Tab to move between answers and Enter or Space to choose one. After answering every question, you can reset the quiz and try again.

You won't get a grade, there's no time limit, and the course doesn't keep a record of your answers.

0 / 22 answered
  1. Question 1of 22

    What happens to s when you call consume(s) with fn consume(s: String)?

    Show a hint

    Think about ownership rules.

  2. Question 2of 22

    Which of these creates a mutable variable in Rust?

  3. Question 3of 22

    What is Option<T> for?

    Show a hint

    Think about what other languages use null for.

  4. Question 4of 22

    Given let value = String::from("hello");, how do you borrow this String immutably?

  5. Question 5of 22

    What happens when this inner scope ends?

    let name = String::from("Ada");
    {
        let moved = name;
    }
    // Can we use name here?
    
  6. Question 6of 22

    What's the difference between String and &str?

  7. Question 7of 22

    What does #[derive(Debug)] do?

  8. Question 8of 22

    What does the ? operator do on a Result<T, E>?

    Show a hint

    It collapses a very common match pattern.

  9. Question 9of 22

    Which of these creates an empty Vec?

  10. Question 10of 22

    What happens if you call HashMap::get for a key that doesn't exist?

    Show a hint

    Rust prefers types over panics for expected absence.

  11. Question 11of 22

    How do you create an owned String from a string literal?

  12. Question 12of 22

    Which of these is true of Rust enums?

    Show a hint

    Rust enums are closer to algebraic data types than to C-style enums.

  13. Question 13of 22

    longest returns a reference to whichever input string is longer. Its signature is:

    fn longest<'a>(x: &'a str, y: &'a str) -> &'a str
    

    Will this code compile? Why or why not?

    let first = String::from("a rainy afternoon");
    let result;
    {
        let second = String::from("sun");
        result = longest(&first, &second);
    }
    println!("{result}");
    
    Show a hint

    Which string is dropped at the closing brace? The compiler checks the call using the function's signature, not by comparing the lengths of these strings.

  14. Question 14of 22

    Compare these two clones:

    use std::rc::Rc;
    
    let text = String::from("hello");
    let text_copy = text.clone();
    let shared = Rc::new(String::from("hello"));
    let shared_copy = Rc::clone(&shared);
    

    Rc<T> is a reference-counted pointer: several handles can own the same allocation. Cloning an Rc adds a handle, and the contained value is dropped when the last strong handle is dropped. Which pair still shares the same string allocation?

  15. Question 15of 22

    What is match primarily for?

  16. Question 16of 22

    In { let s = String::from("hello"); }, when is s dropped?

    Show a hint

    Here, s is neither moved nor explicitly dropped before the closing brace.

  17. Question 17of 22

    What does 'static mean in the reference type &'static str?

  18. Question 18of 22

    What is a trait in Rust?

  19. Question 19of 22

    How do you make an item visible from outside its module?

  20. Question 20of 22

    Given let mut x: &i32 = &42;, which line binds r to an i32 rather than a reference?

    Show a hint

    Track the type after each operator. Does the whole expression produce an integer, or borrow one again?

  21. Question 21of 22

    In the CSV project, a comma inside quotes is data, and "" inside a quoted field encodes one literal ". Predict the fields for this row:

    "a""b,c",d
    
    Show a hint

    Trace whether the parser is inside quotes after consuming the escaped pair.

  22. Question 22of 22

    The environment-file project's typed lookup returns Option<T>. For a u16 port, its body could be:

    values.get(key).and_then(|raw| raw.parse::<u16>().ok())
    

    Predict the results when PORT is absent, is "8080", and is "eight", in that order.

    Show a hint

    and_then calls the closure only for Some. .ok() converts a successful Result to Some and an error to None.

Next chapter 24Appendix

Optional Chapters

Extra practice to explore at your own pace. These chapters do not count toward course progress.

← Return to the Rust course curriculum