0
0
Rustprogramming~10 mins

main function and entry point in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the main entry point of a Rust program.

Rust
fn [1]() {
    println!("Hello, world!");
}
Drag options to blanks, or click blank then click option'
Amain
Bstart
Crun
Dbegin
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a function name other than 'main' for the entry point.
Forgetting to define a function at all.
2fill in blank
medium

Complete the code to print a message inside the main function.

Rust
fn main() {
    [1]!("Welcome to Rust!");
}
Drag options to blanks, or click blank then click option'
Aecho
Bprintln
Cprint
Dwrite
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using print which does not add a newline.
Using functions that do not exist in Rust.
3fill in blank
hard

Fix the error in the main function declaration.

Rust
fn [1](args: Vec<String>) {
    println!("Arguments count: {}", args.len());
}
Drag options to blanks, or click blank then click option'
Astart
Brun
Centry
Dmain
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Adding parameters to the main function.
Using a different function name for the entry point.
4fill in blank
hard

Fill both blanks to create a main function that returns a result.

Rust
fn main() -> [1] {
    println!("Program started");
    [2]
}
Drag options to blanks, or click blank then click option'
AResult<(), Box<dyn std::error::Error>>
Bi32
COk(())
D0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Returning an integer when a Result is expected.
Not returning anything from a function with a return type.
5fill in blank
hard

Fill the blanks to create a main function that reads command line arguments and prints the first one.

Rust
fn main() {
    let args: Vec<String> = std::env::[1]().collect();
    if args.len() > 1 {
        println!("First argument: {}", args[[2]]);
    } else {
        println!("No arguments provided.");
    }
}
Drag options to blanks, or click blank then click option'
Aargs
B1
C0
Denv
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the wrong function to get arguments.
Accessing the wrong index for the first user argument.