Bird
Raised Fist0
Rustprogramming~20 mins

Function declaration in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rust Function 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 function call?

Consider the following Rust code. What will be printed when greet("Alice") is called?

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

fn main() {
    greet("Alice");
}
AHello, Alice!
BCompilation error
CHello, "Alice"!
DHello, name!
Attempts:
2 left
💡 Hint

Look at how the println! macro uses curly braces {} to insert the variable.

❓ Predict Output
intermediate
2:00remaining
What is the return value of this Rust function?

What value does the function add return when called with add(3, 4)?

Rust
fn add(x: i32, y: i32) -> i32 {
    x + y
}

fn main() {
    let result = add(3, 4);
    println!("{}", result);
}
ACompilation error
B34
C7
D0
Attempts:
2 left
💡 Hint

The function returns the sum of x and y without a semicolon.

🔧 Debug
advanced
2:00remaining
What error does this Rust function cause?

What error will the Rust compiler report for this function?

Rust
fn multiply(x: i32, y: i32) -> i32 {
    return x * y;
    println!("This line is unreachable");
}

fn main() {
    let result = multiply(2, 3);
    println!("{}", result);
}
AWarning: unreachable code
BError: missing return statement
CError: type mismatch
DNo error, prints 6
Attempts:
2 left
💡 Hint

Think about what happens after a return statement in Rust.

📝 Syntax
advanced
2:00remaining
Which function declaration is syntactically correct in Rust?

Choose the correct Rust function declaration that returns a boolean value.

Afn is_even(num: i32) -> bool { return num % 2 = 0; }
Bfn is_even(num: i32) bool { return num % 2 == 0; }
Cfn is_even(num: i32) -> boolean { num % 2 == 0 }
Dfn is_even(num: i32) -> bool { num % 2 == 0 }
Attempts:
2 left
💡 Hint

Remember Rust uses -> for return type and bool as the boolean type.

🚀 Application
expert
3:00remaining
What is the output of this recursive Rust function?

What will be printed when factorial(4) is called?

Rust
fn factorial(n: u32) -> u32 {
    match n {
        0 => 1,
        _ => n * factorial(n - 1),
    }
}

fn main() {
    println!("{}", factorial(4));
}
ACompilation error
B24
C10
D120
Attempts:
2 left
💡 Hint

Recall factorial of 4 is 4 * 3 * 2 * 1.

Practice

(1/5)
1. What keyword is used to declare a function in Rust?
easy
A. fn
B. function
C. def
D. func

Solution

  1. Step 1: Recall Rust function syntax

    In Rust, functions are declared using the keyword fn.
  2. Step 2: Compare options with Rust syntax

    Only fn matches the correct Rust keyword for function declaration.
  3. Final Answer:

    fn -> Option A
  4. Quick Check:

    Function declaration keyword = fn [OK]
Hint: Remember Rust uses 'fn' to start functions [OK]
Common Mistakes:
  • Using 'function' instead of 'fn'
  • Confusing with Python's 'def'
  • Using 'func' which is not Rust syntax
2. Which of the following is the correct syntax to declare a function named greet that takes no parameters and returns nothing in Rust?
easy
A. def greet() {}
B. fn greet() {}
C. fn greet {}
D. function greet() {}

Solution

  1. Step 1: Check function declaration syntax

    In Rust, a function with no parameters and no return value is declared as fn name() {}.
  2. Step 2: Validate each option

    fn greet() {} matches Rust syntax exactly. Options A and B use keywords from other languages. fn greet {} misses parentheses.
  3. Final Answer:

    fn greet() {} -> Option B
  4. Quick Check:

    Empty parameter list requires () in Rust [OK]
Hint: Functions always need () even if no parameters [OK]
Common Mistakes:
  • Omitting parentheses
  • Using other language keywords
  • Adding unnecessary keywords
3. What will be the output of this Rust code?
fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn main() {
    println!("{}", add(3, 4));
}
medium
A. 7
B. 34
C. a + b
D. Error: missing return

Solution

  1. Step 1: Understand the add function

    The function takes two integers and returns their sum using expression a + b.
  2. Step 2: Trace the main function call

    Calling add(3, 4) returns 7, which is printed by println!.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Sum of 3 and 4 is 7, printed as output [OK]
Common Mistakes:
  • Thinking 'a + b' prints as string
  • Concatenating numbers as strings
  • Assuming missing return error
4. Identify the error in this Rust function declaration:
fn multiply(x i32, y: i32) -> i32 {
    x * y
}
medium
A. Function name is invalid
B. Missing return type
C. Missing colon after parameter x
D. Missing semicolon after multiplication

Solution

  1. Step 1: Check parameter syntax

    Each parameter must have a colon between name and type. Here, x i32 misses the colon.
  2. Step 2: Verify other parts

    Return type is present, function name is valid, and semicolon is not needed after expression in Rust.
  3. Final Answer:

    Missing colon after parameter x -> Option C
  4. Quick Check:

    Parameters need ':' between name and type [OK]
Hint: Parameters need ':' between name and type [OK]
Common Mistakes:
  • Omitting colon in parameters
  • Adding unnecessary semicolons
  • Confusing return type syntax
5. How would you declare a Rust function named is_even that takes an integer parameter and returns a boolean indicating if the number is even?
hard
A. fn is_even(n: i32) -> boolean { n % 2 == 0 }
B. fn is_even(n: i32) { return n % 2 == 0; }
C. fn is_even(n: int) -> bool { n % 2 = 0 }
D. fn is_even(n: i32) -> bool { n % 2 == 0 }

Solution

  1. Step 1: Check parameter and return types

    Parameter type must be i32 and return type bool in Rust.
  2. Step 2: Validate function body syntax

    Expression n % 2 == 0 correctly returns a boolean. fn is_even(n: i32) -> bool { n % 2 == 0 } uses correct syntax and types.
  3. Final Answer:

    fn is_even(n: i32) -> bool { n % 2 == 0 } -> Option D
  4. Quick Check:

    Use i32 and bool with correct expression [OK]
Hint: Use '-> bool' and 'n % 2 == 0' for even check [OK]
Common Mistakes:
  • Using wrong type names like int or boolean
  • Omitting return type
  • Using '=' instead of '==' in condition
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