Unsafe & Macros Questions with Answers

8 practice questions on Unsafe & Macros (Rust) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

Which of these is a macro call rather than a function call?

  1. vec![1, 2]
  2. Vec::new()
  3. String::from("a")
  4. [1, 2]

Answer: A. vec![1, 2]

Explanation

A macro call ends its name with !, as in vec![...]. vec! is a macro that builds a Vec from the list you give it. The other options are a function call, an associated function and an array literal.

Step-by-step solution

  1. Macro calls have a! after the name, such as name!(...) or name![...].
  2. vec![1, 2] has vec! in that form, so it is a macro call.
  3. Vec::new() and String::from are functions, and [1, 2] is an array literal.

Why the other options are wrong

  • B. Vec::new() is an associated function with no !.
  • C. String::from is a function; there is no! in the call.
  • D. [1, 2] is an array literal, not a call.

Tip: If the name ends in !, it is a macro.

Question 2Easy

What does the ! in println! indicate?

  1. It is a macro invocation, not a normal function call
  2. It negates the output being printed
  3. It marks the call as an unsafe operation
  4. It forces a panic whenever an error occurs

Answer: A. It is a macro invocation, not a normal function call

Explanation

Macros are invoked with !. They expand at compile time, which is why println! can take a variable number of arguments.

Step-by-step solution

  1. println! has a bang.
  2. Bang marks macros.
  3. They expand at compile time.

Why the other options are wrong

  • B. The! is not boolean negation here.
  • C. unsafe is a separate keyword.
  • D. Panics are unrelated.

Question 3Easy

What does #[derive(Debug)] add to a struct?

  1. Makes every field public so the struct can be printed
  2. An automatic Copy implementation, so every assignment duplicates the value
  3. A Debug implementation that lets {:?} format the struct
  4. A runtime check that stops integer overflow in the struct's fields

Answer: C. A Debug implementation that lets {:?} format the struct

Explanation

derive asks the compiler to write the Debug trait for the type. That lets the value be printed with {:?} or {:#?}.

Step-by-step solution

  1. derive(Debug) asks the compiler to generate an implementation of the Debug trait.
  2. The generated code describes each field so the value can be printed.
  3. So println!("{:?}", value) works for that struct.

Why the other options are wrong

  • A. derive(Debug) does not change field visibility.
  • B. Copy is a separate trait; it needs derive(Copy) or a manual impl.
  • D. Overflow checks are compiler behaviour; derive(Debug) adds nothing to the fields.

Tip: derive writes the boilerplate for a trait; Debug is what {:?} uses.

Question 4Medium

Which operation requires an unsafe block?

  1. Calling a method on a Vec of integers
  2. Using an ordinary shared reference
  3. Matching on an enum with a match
  4. Dereferencing a raw pointer

Answer: D. Dereferencing a raw pointer

Explanation

Raw pointers aren't tracked by the borrow checker, so dereferencing one is unsafe. The programmer must uphold the safety rules.

Step-by-step solution

  1. Raw pointers skip checks.
  2. Compiler cannot verify.
  3. unsafe block is needed.

Why the other options are wrong

  • A. Vec methods are safe.
  • B. References are checked by the borrow checker.
  • C. Pattern matching is safe.

Question 5Medium

What is printed?

macro_rules! double {
    ($x:expr) => {
        $x * 2
    };
}

fn main() {
    println!("{}", double!(1 + 2));
}
  1. 6
  2. 5
  3. 3
  4. Compile error

Answer: A. 6

Explanation

An expr fragment keeps (1 + 2) as one unit, so the macro expands to (1 + 2) * 2, which is 6. Plain text substitution would give 1 + 2 * 2, which is 5.

Step-by-step solution

  1. double!(1 + 2) matches $x:expr with $x = (1 + 2).
  2. The expansion is $x * 2, and $x stays a single expression.
  3. So the value is (1 + 2) * 2 = 6.

Why the other options are wrong

  • B. That is what plain text substitution would give: 1 + 2 * 2 ignores the grouping.
  • C. 3 is only the $x part, without the multiplication.
  • D. The macro is valid Rust and compiles.

Tip: Macro fragments of type expr keep their grouping; they are not pasted as raw text.

Question 6Medium

What is printed?

fn main() {
    let mut n = 10;
    let p = &mut n as *mut i32;
    unsafe {
        *p += 5;
    }
    println!("{}", n);
}
  1. 10
  2. 5
  3. Compile error
  4. 15

Answer: D. 15

Explanation

Casting a mutable reference to a raw pointer is safe. Writing through the raw pointer needs an unsafe block, and that write changes n to 15.

Step-by-step solution

  1. p is a raw pointer to n, created with a cast.
  2. *p += 5 writes through the pointer, so n goes from 10 to 15.
  3. println! then prints 15.

Why the other options are wrong

  • A. The write through the pointer changes n, so the old value is not printed.
  • B. 5 is the amount added, not the final value of n.
  • C. Creating a raw pointer and writing to it inside unsafe compiles.

Tip: A raw pointer points at the same memory, so writing through it changes the original variable.

Question 7Medium

What does panic! do in a single-threaded program by default?

  1. Returns an Err value to the caller to handle
  2. Prints a message and unwinds the stack, ending the program
  3. Ignores the error and continues with the next line
  4. Retries the last statement until it succeeds

Answer: B. Prints a message and unwinds the stack, ending the program

Explanation

A panic unwinds (or aborts, depending on settings), running destructors on the way, and terminates the thread; in the main thread the program exits with a non-zero code.

Step-by-step solution

  1. panic! signals an unrecoverable error.
  2. Stack unwinds.
  3. Program ends.

Why the other options are wrong

  • A. panic! does not return a Result.
  • C. Execution does not continue.
  • D. There is no retry.

Question 8Medium

Which of these compiles in safe Rust, without an unsafe block?

  1. Dereferencing a raw pointer with *p
  2. Calling an extern "C" function such as abs
  3. Creating a raw pointer with &x as *const i32
  4. Reading a static mut variable

Answer: C. Creating a raw pointer with &x as *const i32

Explanation

Turning a reference into a raw pointer is safe; the pointer is only a value. Dereferencing a raw pointer, calling a foreign function and reading a static mut all need unsafe.

Step-by-step solution

  1. Creating a raw pointer only stores an address, so no unsafe is needed.
  2. Dereferencing a raw pointer may read invalid memory, so it needs unsafe.
  3. Foreign functions and static mut access are also unsafe operations.

Why the other options are wrong

  • A. Dereferencing a raw pointer is one of the operations unsafe is for.
  • B. Calling foreign code is unsafe because the compiler cannot check it.
  • D. Reading a static mut can race or alias, so it needs unsafe.

Tip: Making a raw pointer is safe; using it (dereferencing) is what needs unsafe.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Rust topics · All topics