Rust Cargo and Project Setup Quiz

Rust
0 Passed
0% acceptance

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.

80 Questions
~160 minutes
1

Question 1

What is Cargo in the context of Rust?

A
Rust's package manager and build system
B
A Rust compiler
C
A code editor for Rust
D
A testing framework
2

Question 2

What command creates a new Rust project?

A
cargo new project_name
B
rust new project_name
C
cargo init project_name
D
rustc --new project_name
3

Question 3

What does cargo new create by default?

A
A binary (executable) project
B
A library project
C
An empty directory
D
A web application
4

Question 4

What is the purpose of the --lib flag with cargo new?

A
Creates a library project instead of a binary
B
Adds library dependencies
C
Creates a library documentation
D
Links to external libraries
5

Question 5

What files does cargo new typically create?

A
Cargo.toml and src/main.rs
B
main.rs and lib.rs
C
Cargo.toml only
D
src/ directory only
6

Question 6

What command builds a Rust project?

A
cargo build
B
cargo compile
C
rustc build
D
cargo make
7

Question 7

What is the difference between cargo build and cargo build --release?

A
--release optimizes for performance but takes longer to compile
B
--release creates debug symbols
C
--release only builds dependencies
D
There is no difference
8

Question 8

What command both builds and runs a Rust project?

A
cargo run
B
cargo execute
C
cargo start
D
cargo launch
9

Question 9

What happens if you run cargo run on a project that hasn't been built yet?

A
Cargo automatically builds the project first, then runs it
B
It fails with an error
C
It only runs without building
D
It creates a debug build
10

Question 10

What is the standard Rust project structure created by cargo new?

A
Project root with Cargo.toml and src/ directory
B
src/ with main.rs and lib.rs
C
target/ for build artifacts
D
All of the above
11

Question 11

What is the purpose of the src/ directory in a Rust project?

A
Contains the source code files (.rs files)
B
Stores build artifacts
C
Holds configuration files
D
Contains documentation
12

Question 12

What is Cargo.toml?

A
The manifest file that contains project metadata and dependencies
B
The main source file
C
A build script
D
A documentation file
13

Question 13

What does the [package] section in Cargo.toml contain?

A
Project name, version, authors, and edition
B
Dependencies only
C
Build configuration
D
Test settings
14

Question 14

How do you add a dependency to a Rust project?

A
Add it to the [dependencies] section in Cargo.toml
B
Use cargo add crate_name
C
Import it in the source code
D
Download it manually
15

Question 15

What is the syntax for specifying a dependency version in Cargo.toml?

A
crate_name = "1.0"
B
crate_name = { version = "1.0" }
C
Both A and B are valid
D
Neither is valid
16

Question 16

What does cargo check do?

A
Checks code for compilation errors without producing binaries
B
Runs tests
C
Formats code
D
Updates dependencies
17

Question 17

What is the target/ directory used for?

A
Storing build artifacts and compiled binaries
B
Source code
C
Configuration files
D
Documentation
18

Question 18

How do you specify the Rust edition in Cargo.toml?

A
edition = "2021" in the [package] section
B
rust-edition = "2021"
C
version = "2021"
D
edition is not specified in Cargo.toml
19

Question 19

What command shows information about a Cargo project?

A
cargo info
B
cargo metadata
C
cargo show
D
cargo project
20

Question 20

What is the difference between cargo run and cargo build followed by manual execution?

A
cargo run automatically finds and runs the binary, while manual execution requires knowing the path
B
There is no difference
C
cargo run only works in debug mode
D
Manual execution is faster
21

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?

A
Library project with cargo new --lib
B
Binary project with cargo new
C
Use cargo init instead
D
Create both lib and binary
22

Question 22

What happens when you add a dependency to Cargo.toml?

A
Cargo downloads and compiles the dependency automatically on next build
B
You need to run cargo update
C
The dependency is added to src/
D
Nothing, you must rebuild manually
23

Question 23

How can you see what dependencies your project is using?

A
cargo tree
B
cargo deps
C
cargo list
D
cargo dependencies
24

Question 24

What is the purpose of Cargo.lock?

A
Locks dependency versions to ensure reproducible builds
B
Locks the project configuration
C
Prevents code changes
D
Locks the target directory
25

Question 25

When should you commit Cargo.lock to version control?

A
For binary projects (applications)
B
For library projects
C
Never
D
Only for release builds
26

Question 26

What does cargo clean do?

A
Removes the target/ directory and build artifacts
B
Removes dependencies
C
Removes source files
D
Cleans the Cargo.toml file
27

Question 27

How do you create a new Rust project in an existing directory?

A
cargo init
B
cargo new .
C
cargo create
D
cargo start
28

Question 28

What is the default name for the main source file in a binary project?

A
src/main.rs
B
main.rs
C
src/lib.rs
D
lib.rs
29

Question 29

In a scenario where your project needs both a library and a binary, how should you structure it?

A
Use src/lib.rs for the library and src/main.rs for the binary
B
Use separate Cargo.toml files
C
Use src/main.rs for both
D
This is not possible in Rust
30

Question 30

What command updates dependencies to their latest compatible versions?

A
cargo update
B
cargo upgrade
C
cargo refresh
D
cargo sync
31

Question 31

How do you specify a Git dependency in Cargo.toml?

A
crate_name = { git = "https://github.com/user/repo" }
B
crate_name = "git+https://github.com/user/repo"
C
crate_name = { url = "https://github.com/user/repo" }
D
This is not supported
32

Question 32

What is the benefit of using cargo check during development?

A
Faster feedback on compilation errors without full compilation
B
It runs tests
C
It formats code
D
It builds release binaries
33

Question 33

In a team project, how should you handle Cargo.lock?

A
For applications: commit it; for libraries: don't commit it
B
Always commit it
C
Never commit it
D
Commit only the first version
34

Question 34

What does the --verbose flag do with Cargo commands?

A
Shows detailed output including compiler invocations
B
Makes commands run faster
C
Only shows errors
D
Formats the output
35

Question 35

How can you build a project for a different target platform?

A
cargo build --target target-triple
B
cargo cross-compile
C
cargo build --platform
D
This requires separate compilation
36

Question 36

What is the purpose of the [workspace] section in Cargo.toml?

A
Defines a workspace containing multiple related packages
B
Configures the build workspace
C
Sets up testing workspaces
D
Manages documentation
37

Question 37

In a scenario where you need to temporarily disable a dependency, how can you do it?

A
Comment it out in Cargo.toml
B
Use cargo remove
C
Delete it from target/
D
Use cargo disable
38

Question 38

What information does cargo --version show?

A
Cargo version, commit hash, and release date
B
Only the version number
C
Rust compiler version
D
Project version
39

Question 39

How do you view documentation for a dependency?

A
cargo doc --open
B
cargo docs
C
cargo help
D
cargo info
40

Question 40

What is the most important benefit of using Cargo for Rust development?

A
It handles dependency management, building, testing, and documentation automatically
B
It makes Rust code run faster
C
It replaces the need for source code
D
It only works with binary projects
41

Question 41

What will this Rust code print?

rust
fn main() {
    let x = 5;
    let y = x;
    println!("x = {}, y = {}", x, y);
}
A
x = 5, y = 5
B
Compilation error
C
x = 5, y = 0
D
Runtime error
42

Question 42

What happens when you try to compile this Rust code?

rust
fn main() {
    let s1 = String::from("hello");
    let s2 = s1;
    println!("s1 = {}", s1);
}
A
Prints: s1 = hello
B
Compilation error: borrow of moved value
C
Prints: s1 =
D
Runtime panic
43

Question 43

How do you fix the borrowing issue in this code?

rust
fn main() {
    let s = String::from("hello");
    let len = calculate_length(s);
    println!("Length: {}", len);
}

fn calculate_length(s: String) -> usize {
    s.len()
}
A
Use &String instead of String
B
Return both the string and length
C
Clone the string
D
Use Rc<String>
44

Question 44

What does this Rust code demonstrate?

rust
fn main() {
    let mut s = String::from("hello");
    change(&mut s);
    println!("{}", s);
}

fn change(some_string: &mut String) {
    some_string.push_str(", world");
}
A
Mutable borrowing
B
Ownership transfer
C
Immutable borrowing
D
Copy semantics
45

Question 45

What will this code output?

rust
fn main() {
    let x = 5;
    let y = &x;
    println!("x = {}, y = {}", x, y);
}
A
x = 5, y = 5
B
Compilation error
C
x = 5, y = reference
D
Runtime error
46

Question 46

What is the purpose of an if expression in Rust?

A
To execute code conditionally based on a boolean condition
B
To create loops
C
To define functions
D
To declare variables
47

Question 47

What will this if expression evaluate to?

rust
fn main() {
    let condition = true;
    let number = if condition { 5 } else { 6 };
    println!("The number is: {}", number);
}
A
The number is: 5
B
The number is: 6
C
Compilation error
D
Runtime error
48

Question 48

Can if expressions be used on the right side of an assignment?

A
Yes, because if expressions return values
B
No, if expressions don't return values
C
Only in certain contexts
D
Only with else branches
49

Question 49

What happens if you omit the else branch in an if expression used in an assignment?

A
Compilation error: if expressions must have else when used as values
B
The expression returns () (unit)
C
The expression returns the if condition
D
Runtime error
50

Question 50

How do you write a multi-condition if-else if chain?

A
if condition1 { ... } else if condition2 { ... } else { ... }
B
if condition1 && condition2 { ... } else { ... }
C
switch condition1 { case condition2: ... }
D
if condition1 { ... } and if condition2 { ... }
51

Question 51

What is if let syntax used for?

A
Matching patterns while checking a condition
B
Creating new variables
C
Looping with conditions
D
Type conversion
52

Question 52

What will this code output?

rust
fn main() {
    let some_option = Some(5);
    if let Some(value) = some_option {
        println!("Got value: {}", value);
    } else {
        println!("No value");
    }
}
A
Got value: 5
B
No value
C
Compilation error
D
Runtime error
53

Question 53

What is the basic syntax of a loop expression?

A
loop { ... }
B
for i in 0..10 { ... }
C
while condition { ... }
D
repeat 10 { ... }
54

Question 54

How do you exit a loop prematurely?

A
Using the break keyword
B
Using the exit keyword
C
Using the return keyword
D
Loops cannot be exited prematurely
55

Question 55

What does the continue keyword do in a loop?

A
Skips the rest of the current iteration and starts the next one
B
Exits the loop entirely
C
Restarts the loop from the beginning
D
Pauses the loop execution
56

Question 56

Can loop expressions return values?

A
Yes, by using break with a value: break value;
B
No, loops don't return values
C
Only infinite loops can return values
D
Only with the return keyword
57

Question 57

What will this loop code output?

rust
fn main() {
    let mut counter = 0;
    let result = loop {
        counter += 1;
        if counter == 3 {
            break counter * 2;
        }
    };
    println!("Result: {}", result);
}
A
Result: 6
B
Result: 3
C
Compilation error
D
Infinite loop
58

Question 58

What is a while loop used for?

A
Looping while a condition remains true
B
Looping a fixed number of times
C
Looping infinitely
D
Looping over collections
59

Question 59

What will this while loop do?

rust
fn main() {
    let mut number = 3;
    while number != 0 {
        println!("{}!", number);
        number -= 1;
    }
    println!("LIFTOFF!!!");
}
A
Print 3!, 2!, 1!, LIFTOFF!!!
B
Print 3!, 2!, 1!, 0!, LIFTOFF!!!
C
Infinite loop
D
Compilation error
60

Question 60

What is while let syntax used for?

A
Looping while a pattern matches
B
Creating variables in loops
C
Type conversion in loops
D
Infinite looping
61

Question 61

What is the syntax for a for loop in Rust?

A
for element in collection { ... }
B
for (int i = 0; i < 10; i++) { ... }
C
for i in 0..10 { ... }
D
All of the above
62

Question 62

What does 0..10 represent in a for loop?

A
A range from 0 (inclusive) to 10 (exclusive)
B
A range from 0 to 10 inclusive
C
An array of numbers
D
A floating-point range
63

Question 63

What will this for loop output?

rust
fn main() {
    for number in (1..4).rev() {
        println!("{}!", number);
    }
    println!("LIFTOFF!!!");
}
A
3!, 2!, 1!, LIFTOFF!!!
B
1!, 2!, 3!, LIFTOFF!!!
C
4!, 3!, 2!, LIFTOFF!!!
D
Compilation error
64

Question 64

In a scenario where you're iterating over array elements to find a specific value, which loop construct is most appropriate?

A
for element in &array { ... }
B
while let Some(element) = array.next() { ... }
C
loop with manual indexing
D
for i in 0..array.len() { ... }
65

Question 65

What happens when you use break in a nested loop?

A
It breaks out of the innermost loop only
B
It breaks out of all loops
C
Compilation error
D
It breaks out of the outermost loop
66

Question 66

How can you break out of an outer loop from within a nested inner loop?

A
Use labeled breaks: break 'outer_loop;
B
Use return to exit the function
C
Use continue with a label
D
This is not possible
67

Question 67

What will this labeled break code do?

rust
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");
}
A
Print 'Entered outer loop', 'Entered inner loop', 'Exited both loops'
B
Print 'Entered outer loop', 'Entered inner loop', 'This will not print', 'Exited both loops'
C
Compilation error
D
Infinite loop
68

Question 68

When should you use continue in a loop?

A
When you want to skip the current iteration and move to the next one
B
When you want to exit the loop
C
When you want to restart the loop
D
When you want to pause execution
69

Question 69

Can continue be used with labels?

A
Yes, to continue an outer loop from an inner loop
B
No, continue only works on the current loop
C
Only in while loops
D
Only in for loops
70

Question 70

In a scenario where you're processing a list of items but want to skip invalid ones, which approach is most appropriate?

A
Use continue to skip invalid items
B
Use break to exit on invalid items
C
Use return to skip items
D
Remove invalid items from the list first
71

Question 71

What is the difference between break and return in a loop?

A
break exits the loop, return exits the function
B
return exits the loop, break exits the function
C
They are the same
D
break only works in loops, return only in functions
72

Question 72

What will this code output?

rust
fn main() {
    let mut i = 0;
    loop {
        i += 1;
        if i == 2 {
            continue;
        }
        if i == 4 {
            break;
        }
        println!("i = {}", i);
    }
}
A
i = 1, i = 3
B
i = 1, i = 2, i = 3
C
i = 1, i = 3, i = 4
D
Infinite loop
73

Question 73

How do you iterate over a vector in reverse order?

A
for element in vec.iter().rev() { ... }
B
for element in vec.rev() { ... }
C
for i in (0..vec.len()).rev() { ... }
D
All of the above
74

Question 74

What is the most common use case for loop expressions?

A
When you need an infinite loop that breaks based on internal conditions
B
When iterating over collections
C
When you know the number of iterations
D
When looping while a condition is true
75

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?

A
loop { ... }
B
while running { ... }
C
for _ in 0.. { ... }
D
Any of the above could work
76

Question 76

What is if let useful for when working with Options?

A
Conditionally executing code when an Option is Some(value)
B
Converting Options to Results
C
Creating new Options
D
Unwrapping Options unsafely
77

Question 77

How does Rust prevent infinite loops in while loops?

A
It doesn't - you must ensure the condition eventually becomes false
B
It automatically adds break conditions
C
It requires loop bounds
D
It uses timeouts
78

Question 78

What will this code do?

rust
fn main() {
    let numbers = vec![1, 2, 3, 4, 5];
    for num in &numbers {
        if *num == 3 {
            break;
        }
        println!("num = {}", num);
    }
}
A
Print num = 1, num = 2
B
Print num = 1, num = 2, num = 3
C
Compilation error
D
Print all numbers
79

Question 79

When should you prefer for loops over while loops?

A
When iterating over collections or ranges
B
When you need manual index control
C
When the number of iterations is unknown
D
When you need infinite loops
80

Question 80

What is the key difference between if and while let?

A
if let executes once, while let executes repeatedly while the pattern matches
B
while let executes once, if let executes repeatedly
C
They are identical
D
if let is for loops, while let is for conditions

QUIZZES IN Rust