Rust Cargo and Project Setup Quiz
40 comprehensive questions on Rust's package manager Cargo, covering project initialization, building, running, project structure, and dependency management — with 15 code examples demonstrating practical Cargo usage.
Question 1
What is Cargo in the context of Rust?
Question 2
What command creates a new Rust project?
Question 3
What does cargo new create by default?
Question 4
What is the purpose of the --lib flag with cargo new?
Question 5
What files does cargo new typically create?
Question 6
What command builds a Rust project?
Question 7
What is the difference between cargo build and cargo build --release?
Question 8
What command both builds and runs a Rust project?
Question 9
What happens if you run cargo run on a project that hasn't been built yet?
Question 10
What is the standard Rust project structure created by cargo new?
Question 11
What is the purpose of the src/ directory in a Rust project?
Question 12
What is Cargo.toml?
Question 13
What does the [package] section in Cargo.toml contain?
Question 14
How do you add a dependency to a Rust project?
Question 15
What is the syntax for specifying a dependency version in Cargo.toml?
Question 16
What does cargo check do?
Question 17
What is the target/ directory used for?
Question 18
How do you specify the Rust edition in Cargo.toml?
Question 19
What command shows information about a Cargo project?
Question 20
What is the difference between cargo run and cargo build followed by manual execution?
Question 21
In a scenario where you're working on a Rust library that will be used by multiple projects, what type of Cargo project should you create?
Question 22
What happens when you add a dependency to Cargo.toml?
Question 23
How can you see what dependencies your project is using?
Question 24
What is the purpose of Cargo.lock?
Question 25
When should you commit Cargo.lock to version control?
Question 26
What does cargo clean do?
Question 27
How do you create a new Rust project in an existing directory?
Question 28
What is the default name for the main source file in a binary project?
Question 29
In a scenario where your project needs both a library and a binary, how should you structure it?
Question 30
What command updates dependencies to their latest compatible versions?
Question 31
How do you specify a Git dependency in Cargo.toml?
Question 32
What is the benefit of using cargo check during development?
Question 33
In a team project, how should you handle Cargo.lock?
Question 34
What does the --verbose flag do with Cargo commands?
Question 35
How can you build a project for a different target platform?
Question 36
What is the purpose of the [workspace] section in Cargo.toml?
Question 37
In a scenario where you need to temporarily disable a dependency, how can you do it?
Question 38
What information does cargo --version show?
Question 39
How do you view documentation for a dependency?
Question 40
What is the most important benefit of using Cargo for Rust development?
Question 41
What will this Rust code print?
fn main() {
let x = 5;
let y = x;
println!("x = {}, y = {}", x, y);
}Question 42
What happens when you try to compile this Rust code?
fn main() {
let s1 = String::from("hello");
let s2 = s1;
println!("s1 = {}", s1);
}Question 43
How do you fix the borrowing issue in this code?
fn main() {
let s = String::from("hello");
let len = calculate_length(s);
println!("Length: {}", len);
}
fn calculate_length(s: String) -> usize {
s.len()
}Question 44
What does this Rust code demonstrate?
fn main() {
let mut s = String::from("hello");
change(&mut s);
println!("{}", s);
}
fn change(some_string: &mut String) {
some_string.push_str(", world");
}Question 45
What will this code output?
fn main() {
let x = 5;
let y = &x;
println!("x = {}, y = {}", x, y);
}Question 46
What is the purpose of an if expression in Rust?
Question 47
What will this if expression evaluate to?
fn main() {
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The number is: {}", number);
}Question 48
Can if expressions be used on the right side of an assignment?
Question 49
What happens if you omit the else branch in an if expression used in an assignment?
Question 50
How do you write a multi-condition if-else if chain?
Question 51
What is if let syntax used for?
Question 52
What will this code output?
fn main() {
let some_option = Some(5);
if let Some(value) = some_option {
println!("Got value: {}", value);
} else {
println!("No value");
}
}Question 53
What is the basic syntax of a loop expression?
Question 54
How do you exit a loop prematurely?
Question 55
What does the continue keyword do in a loop?
Question 56
Can loop expressions return values?
Question 57
What will this loop code output?
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 3 {
break counter * 2;
}
};
println!("Result: {}", result);
}Question 58
What is a while loop used for?
Question 59
What will this while loop do?
fn main() {
let mut number = 3;
while number != 0 {
println!("{}!", number);
number -= 1;
}
println!("LIFTOFF!!!");
}Question 60
What is while let syntax used for?
Question 61
What is the syntax for a for loop in Rust?
Question 62
What does 0..10 represent in a for loop?
Question 63
What will this for loop output?
fn main() {
for number in (1..4).rev() {
println!("{}!", number);
}
println!("LIFTOFF!!!");
}Question 64
In a scenario where you're iterating over array elements to find a specific value, which loop construct is most appropriate?
Question 65
What happens when you use break in a nested loop?
Question 66
How can you break out of an outer loop from within a nested inner loop?
Question 67
What will this labeled break code do?
fn main() {
'outer: loop {
println!("Entered outer loop");
'inner: loop {
println!("Entered inner loop");
break 'outer;
}
println!("This will not print");
}
println!("Exited both loops");
}Question 68
When should you use continue in a loop?
Question 69
Can continue be used with labels?
Question 70
In a scenario where you're processing a list of items but want to skip invalid ones, which approach is most appropriate?
Question 71
What is the difference between break and return in a loop?
Question 72
What will this code output?
fn main() {
let mut i = 0;
loop {
i += 1;
if i == 2 {
continue;
}
if i == 4 {
break;
}
println!("i = {}", i);
}
}Question 73
How do you iterate over a vector in reverse order?
Question 74
What is the most common use case for loop expressions?
Question 75
In a scenario where you're implementing a game loop that should run until the player quits, which loop construct is most appropriate?
Question 76
What is if let useful for when working with Options?
Question 77
How does Rust prevent infinite loops in while loops?
Question 78
What will this code do?
fn main() {
let numbers = vec![1, 2, 3, 4, 5];
for num in &numbers {
if *num == 3 {
break;
}
println!("num = {}", num);
}
}Question 79
When should you prefer for loops over while loops?
Question 80
What is the key difference between if and while let?
