Rust Functions Quiz
40 questions covering function parameters, return values, expressions, diverging functions, and function scope in Rust.
Question 1
What is the syntax for defining a function in Rust?
Question 2
How do you specify parameters in a Rust function?
Question 3
What does the -> syntax indicate in a function signature?
Question 4
What happens if you don't specify a return type for a function?
Question 5
How do you explicitly return a value from a function?
Question 6
What is a statement in Rust?
Question 7
What is an expression in Rust?
Question 8
Can function bodies contain both statements and expressions?
Question 9
What will this function return?
fn example() -> i32 {
let x = 5;
x + 1
}Question 10
What will this function return?
fn example() -> i32 {
let x = 5;
x + 1;
}Question 11
What are diverging functions in Rust?
Question 12
What is the return type of a diverging function?
Question 13
Which of these is an example of a diverging function?
Question 14
How does function scope affect variable lifetime?
Question 15
What happens to variables when a function returns?
Question 16
Can functions be defined inside other functions?
Question 17
What is the scope of a variable declared inside a function?
Question 18
How do you pass ownership of a value to a function?
Question 19
How do you allow a function to borrow a value without taking ownership?
Question 20
What happens to borrowed values when a function returns?
Question 21
Can a function modify a borrowed value?
Question 22
What is the difference between String and &str parameters?
Question 23
When should you use &str instead of String in function parameters?
Question 24
What will this function do?
fn take_ownership(s: String) {
println!("{}", s);
} // s is dropped hereQuestion 25
What will this function do?
fn borrow_string(s: &String) {
println!("{}", s);
} // s remains borrowedQuestion 26
How do you return a value from a function while maintaining ownership?
Question 27
What happens if you try to return a reference to a local variable?
Question 28
How can you return a string from a function?
Question 29
What is function overloading in Rust?
Question 30
How does Rust handle functions with the same name?
Question 31
What are generic functions in Rust?
Question 32
What is the syntax for generic functions?
Question 33
Can functions have multiple generic parameters?
Question 34
What are trait bounds in generic functions?
Question 35
What is the syntax for trait bounds?
Question 36
What happens if you call a diverging function?
Question 37
How can diverging functions be useful?
Question 38
What is the scope hierarchy in Rust?
Question 39
Can inner scopes access variables from outer scopes?
Question 40
What happens to variables shadowed in inner scopes?
