Bird
Raised Fist0
Rustprogramming~20 mins

Writing first Rust program - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust First Program Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust program?

Look at this Rust code. What will it print when run?

Rust
fn main() {
    let x = 5;
    let y = 10;
    println!("Sum is: {}", x + y);
}
ASum is: 5 + 10
BCompilation error
CSum is: 510
DSum is: 15
Attempts:
2 left
๐Ÿ’ก Hint

Remember, the + operator adds numbers, not strings.

โ“ Predict Output
intermediate
2:00remaining
What will this Rust program print?

Check this Rust code. What is the output?

Rust
fn main() {
    let name = "Alice";
    println!("Hello, {}!", name);
}
AHello, Alice!
BRuntime error
CHello, {}!
DHello, name!
Attempts:
2 left
๐Ÿ’ก Hint

The curly braces {} are replaced by the variable value.

โ“ Predict Output
advanced
2:00remaining
What is the output of this Rust program with a conditional?

What will this Rust program print?

Rust
fn main() {
    let number = 7;
    if number % 2 == 0 {
        println!("Even");
    } else {
        println!("Odd");
    }
}
ACompilation error
BEven
COdd
D7
Attempts:
2 left
๐Ÿ’ก Hint

Check if 7 is divisible by 2 without remainder.

โ“ Predict Output
advanced
2:00remaining
What does this Rust program print with a loop?

Look at this Rust code. What will it print?

Rust
fn main() {
    let mut count = 0;
    while count < 3 {
        println!("Count: {}", count);
        count += 1;
    }
}
A
Count: 0
Count: 1
Count: 2
B
Count: 0
Count: 1
Count: 2
Count: 3
C
Count: 1
Count: 2
Count: 3
DInfinite loop
Attempts:
2 left
๐Ÿ’ก Hint

The loop runs while count is less than 3, starting from 0.

โ“ Predict Output
expert
2:00remaining
What is the output of this Rust program using a function?

What will this Rust program print?

Rust
fn greet(name: &str) {
    println!("Hello, {}!", name);
}

fn main() {
    let user = String::from("Bob");
    greet(&user);
}
AHello, user!
BHello, Bob!
CCompilation error due to borrowing
DRuntime error
Attempts:
2 left
๐Ÿ’ก Hint

Check how the string is passed to the function.

Practice

(1/5)
1. What is the correct way to start a basic Rust program?
easy
A. By defining a fn main() function
B. By writing start() function
C. By using begin() function
D. By declaring a program() function

Solution

  1. Step 1: Understand Rust program entry point

    Rust programs always start execution from the main function.
  2. Step 2: Identify the correct function name

    The correct function to start a Rust program is fn main().
  3. Final Answer:

    By defining a fn main() function -> Option A
  4. Quick Check:

    Rust program entry point = fn main() [OK]
Hint: Rust programs always start with fn main() [OK]
Common Mistakes:
  • Using incorrect function names like start() or begin()
  • Omitting the main function entirely
  • Confusing Rust with other languages' entry points
2. Which of the following is the correct syntax to print "Hello, world!" in Rust?
easy
A. print!("Hello, world!");
B. println!("Hello, world!");
C. echo("Hello, world!");
D. console.log("Hello, world!");

Solution

  1. Step 1: Identify Rust's print macro

    Rust uses the macro println! to print text with a newline.
  2. Step 2: Compare options

    Only println!("Hello, world!"); is valid Rust syntax for printing with a newline.
  3. Final Answer:

    println!("Hello, world!"); -> Option B
  4. Quick Check:

    Print with newline = println! macro [OK]
Hint: Use println! to print with a newline in Rust [OK]
Common Mistakes:
  • Using print! without newline when newline is expected
  • Using functions from other languages like echo or console.log
  • Missing the exclamation mark after println
3. What will be the output of this Rust program?
fn main() {
    println!("{}", 5 + 3);
}
medium
A. 8
B. 5 + 3
C. {}
D. Compilation error

Solution

  1. Step 1: Understand the println! macro with placeholders

    The {} inside the string is a placeholder for the value following the string.
  2. Step 2: Calculate the expression inside println!

    The expression 5 + 3 evaluates to 8, which replaces the {}.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    5 + 3 = 8 printed [OK]
Hint: {} prints evaluated expression inside println! [OK]
Common Mistakes:
  • Expecting the literal string '5 + 3' to print
  • Confusing {} as a string literal
  • Thinking it causes a compile error
4. Identify the error in this Rust program:
fn main() {
    println!("Hello, world!")
}
medium
A. Missing quotation marks around string
B. Incorrect function name main
C. Missing semicolon after println! macro
D. println! macro does not exist

Solution

  1. Step 1: Check syntax of println! macro call

    In Rust, statements must end with a semicolon unless they are the last expression in a block.
  2. Step 2: Identify missing semicolon

    The println!("Hello, world!") line is missing a semicolon at the end.
  3. Final Answer:

    Missing semicolon after println! macro -> Option C
  4. Quick Check:

    Statements need semicolon unless last expression [OK]
Hint: Add semicolon after println! unless last expression [OK]
Common Mistakes:
  • Forgetting semicolon after macro calls
  • Thinking main function name is wrong
  • Assuming println! is not a macro
5. You want to write a Rust program that prints "Hello, <name>!" where <name> is a variable. Which code correctly does this?
hard
A. fn main() { let name = "Alice"; println!("Hello, {name}!"); }
B. fn main() { let name = "Alice"; println!("Hello, name!"); }
C. fn main() { let name = "Alice"; print!("Hello, {}!", name); }
D. fn main() { let name = "Alice"; println!("Hello, {}!", name); }

Solution

  1. Step 1: Understand variable interpolation in println!

    Rust uses {} as a placeholder and variables are passed after the string.
  2. Step 2: Check each option for correct syntax and output

    fn main() { let name = "Alice"; println!("Hello, {}!", name); } correctly uses println!("Hello, {}!", name); to print the variable's value.
  3. Final Answer:

    fn main() { let name = "Alice"; println!("Hello, {}!", name); } -> Option D
  4. Quick Check:

    Use {} and pass variable to println! [OK]
Hint: Use {} and pass variable to println! for interpolation [OK]
Common Mistakes:
  • Putting variable name inside quotes without {}
  • Using print! instead of println! (missing newline)
  • Trying to use {name} without Rust 1.58+ raw identifiers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard