Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The main() function is the required entry point of a program in Rust. There can be only one main() function in a program.

With the following command we create a new folder named my_program initialized as a Cargo binary (i.e.: program, not library) project:

    Creating binary (application) package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

We enter the folder:

And look at its content:

.
├── Cargo.toml
└── src
    └── main.rs

In the src folder is a single file named main.rs that contains a function main() with a default implementation that prints a message:

fn main() {
    println!("Hello, world!");
}

We may build the program:

   Compiling my_program v0.1.0 (/tmp/tmp.bA4XmK1WjO/my_program)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s

And run it through Cargo:

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/my_program`
Hello, world!

see solution