Rust Borrowing Quiz

Rust
0 Passed
0% acceptance

45 in-depth questions covering Rust's borrowing system, immutable and mutable references, aliasing rules, reference scopes, and borrow checker validation — with 13 code examples to solidify understanding.

45 Questions
~90 minutes
1

Question 1

What is borrowing in Rust?

A
Transferring ownership permanently
B
Granting temporary access to a value without transferring ownership
C
Copying values implicitly
D
Moving values between threads
2

Question 2

What is the syntax for creating an immutable reference?

A
&value
B
&mut value
C
*value
D
ref value
3

Question 3

What is the syntax for creating a mutable reference?

A
&value
B
&mut value
C
*value
D
mut &value
4

Question 4

How many immutable references to the same value can exist simultaneously?

A
Zero
B
One
C
As many as you want
D
Only in the same scope
5

Question 5

Can you have a mutable reference and an immutable reference to the same value at the same time?

A
Yes, always
B
No, never
C
Only if they are in different scopes
D
Only for primitive types
6

Question 6

What will this code do?

rust
fn main() {
    let mut x = 5;
    let r1 = &x;
    let r2 = &x;
    println!("r1 = {}, r2 = {}", r1, r2);
}
A
Print 'r1 = 5, r2 = 5'
B
Compilation error
C
Runtime panic
D
Only r1 is valid
7

Question 7

What will this code do?

rust
fn main() {
    let mut x = 5;
    let r1 = &mut x;
    let r2 = &x;
    println!("r1 = {}, r2 = {}", r1, r2);
}
A
Print 'r1 = 5, r2 = 5'
B
Compilation error
C
Runtime panic
D
Only r1 is valid
8

Question 8

What are the borrowing rules in Rust?

A
One mutable reference OR multiple immutable references
B
Multiple mutable references allowed
C
References can outlive the owner
D
No restrictions on references
9

Question 9

What happens when you try to modify a value through an immutable reference?

A
It works
B
Compilation error
C
Runtime panic
D
The value becomes mutable
10

Question 10

What is dereferencing in Rust?

A
Creating a reference
B
Accessing the value a reference points to
C
Moving ownership
D
Copying a value
11

Question 11

What will this code print?

rust
fn main() {
    let x = 5;
    let r = &x;
    println!("x = {}, r = {}", x, *r);
}
A
x = 5, r = 5
B
Compilation error
C
x = 5, r = address
D
Runtime error
12

Question 12

Can you dereference a mutable reference to modify the value?

A
Yes, with *mut_ref = new_value
B
No, mutable references can't be dereferenced
C
Only for primitive types
D
Only in unsafe code
13

Question 13

What is the scope of a reference?

A
From creation until the end of the program
B
From creation until the referenced value is dropped
C
From creation until the reference is used
D
References have no scope
14

Question 14

What will this code do?

rust
fn main() {
    let r;
    {
        let x = 5;
        r = &x;
    }
    println!("{}", r);
}
A
Print 5
B
Compilation error
C
Runtime panic
D
Print garbage value
15

Question 15

What is the borrow checker?

A
A runtime garbage collector
B
A compile-time analyzer that enforces borrowing rules
C
A linter for style issues
D
A debugger for reference errors
16

Question 16

When does the borrow checker run?

A
At runtime
B
At compile time
C
When the program starts
D
Only when there are errors
17

Question 17

What is a dangling reference?

A
A reference that points to a valid value
B
A reference that points to memory that has been freed
C
A reference that is never used
D
A reference to a constant
18

Question 18

How does Rust prevent dangling references?

A
Garbage collection
B
Lifetime checking
C
Runtime bounds checking
D
Manual memory management
19

Question 19

What will this code do?

rust
fn main() {
    let mut s = String::from("hello");
    let r1 = &s;
    let r2 = &s;
    s.push_str(" world");
    println!("{} {}", r1, r2);
}
A
Print 'hello world hello world'
B
Compilation error
C
Runtime panic
D
Print 'hello hello'
20

Question 20

What will this code do?

rust
fn main() {
    let mut s = String::from("hello");
    let r = &mut s;
    r.push_str(" world");
    println!("{}", r);
}
A
Print 'hello world'
B
Compilation error
C
Runtime panic
D
Print 'hello'
21

Question 21

Can you have multiple mutable references to the same value?

A
Yes, always
B
No, never
C
Only in different scopes
D
Only for primitive types
22

Question 22

What is the difference between &T and &mut T?

A
No difference
B
&T allows reading, &mut T allows reading and writing
C
&T is for primitives, &mut T is for structs
D
&T moves ownership, &mut T borrows
23

Question 23

What will this code do?

rust
fn main() {
    let mut x = 5;
    {
        let y = &mut x;
        *y = 10;
    }
    println!("{}", x);
}
A
Print 10
B
Print 5
C
Compilation error
D
Runtime panic
24

Question 24

What happens to references when they go out of scope?

A
They are dropped
B
The referenced value is dropped
C
Memory is leaked
D
They become invalid
25

Question 25

Can you return a reference from a function?

A
Yes, always
B
No, never
C
Only if the referenced value is static
D
Only for primitive types
26

Question 26

What will this code do?

rust
fn return_ref() -> &String {
    let s = String::from("hello");
    &s
}

fn main() {
    let r = return_ref();
    println!("{}", r);
}
A
Print 'hello'
B
Compilation error
C
Runtime panic
D
Undefined behavior
27

Question 27

What is implicit dereferencing?

A
Automatically adding * when accessing fields
B
Converting references to values
C
Moving ownership
D
Copying values
28

Question 28

What will this code print?

rust
fn main() {
    let s = String::from("hello");
    let r = &s;
    println!("Length: {}", r.len());
}
A
Length: 5
B
Compilation error
C
Runtime panic
D
Length: 0
29

Question 29

What are the benefits of borrowing over ownership?

A
Faster performance
B
Avoiding unnecessary copies
C
Allowing multiple access patterns
D
All of the above
30

Question 30

When should you use borrowing instead of ownership?

A
When you only need temporary access
B
When the caller needs to use the value again
C
When you want to avoid copies
D
All of the above
31

Question 31

What will this code do?

rust
fn main() {
    let mut vec = vec![1, 2, 3];
    let first = &vec[0];
    vec.push(4);
    println!("{}", first);
}
A
Print 1
B
Compilation error
C
Runtime panic
D
Print 4
32

Question 32

What is reference aliasing?

A
Creating multiple names for the same reference
B
Having multiple references to the same value
C
Moving ownership
D
Copying values
33

Question 33

Why does Rust restrict aliasing with mutation?

A
Performance reasons
B
To prevent data races and maintain safety
C
Because it's complicated to implement
D
To force ownership transfers
34

Question 34

What is the NLL (Non-Lexical Lifetimes) feature?

A
A garbage collector
B
More precise lifetime analysis
C
A new reference type
D
Runtime borrow checking
35

Question 35

What will this code do with NLL?

rust
fn main() {
    let mut x = 5;
    let r = &x;
    println!("{}", r);
    let y = &mut x;
    *y = 10;
    println!("{}", y);
}
A
Compilation error
B
Print 5 then 10
C
Runtime panic
D
Print 10 then 10
36

Question 36

What is a borrow split?

A
Dividing a reference into smaller parts
B
Creating separate borrows for different parts of a value
C
Moving ownership partially
D
Copying part of a value
37

Question 37

What will this code do?

rust
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let mut p = Point { x: 1, y: 2 };
    let rx = &mut p.x;
    let ry = &mut p.y;
    *rx = 10;
    *ry = 20;
    println!("{} {}", p.x, p.y);
}
A
Print '10 20'
B
Compilation error
C
Runtime panic
D
Print '1 2'
38

Question 38

What is the purpose of the RefCell<T> type?

A
To bypass borrow checking at runtime
B
To create immutable references
C
To move ownership
D
To copy values
39

Question 39

When should you use RefCell<T>?

A
Always, for performance
B
When you need interior mutability in single-threaded code
C
For all mutable data
D
Never, it's unsafe
40

Question 40

What is the difference between & and &mut borrows?

A
Performance only
B
& allows shared access, &mut allows exclusive access
C
Syntax only
D
& moves, &mut borrows
41

Question 41

What will this code do?

rust
fn main() {
    let s = String::from("hello");
    let len = calculate_length(&s);
    println!("Length of '{}' is {}", s, len);
}

fn calculate_length(s: &String) -> usize {
    s.len()
}
A
Print 'Length of hello is 5'
B
Compilation error
C
Runtime panic
D
Print garbage
42

Question 42

Why is borrowing important for function parameters?

A
Performance
B
Allows the caller to retain ownership
C
Prevents copies
D
All of the above
43

Question 43

What is the relationship between borrowing and lifetimes?

A
They are unrelated
B
Lifetimes track how long borrows are valid
C
Borrowing creates lifetimes
D
Lifetimes replace borrowing
44

Question 44

What is the main advantage of Rust's borrowing system over garbage collection?

A
Faster runtime performance
B
Compile-time memory safety guarantees
C
Simpler syntax
D
Automatic optimization
45

Question 45

When implementing a library API, how should you design borrowing in your function signatures?

A
Always take ownership
B
Always borrow
C
Use borrowing when the function doesn't need ownership, ownership when it consumes the value
D
Use Rc<T> for everything

QUIZZES IN Rust