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:
cargo init my_program 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:
cd my_programAnd look at its content:
eza -T ..
├── 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:
bat src/main.rsfn main() {
println!("Hello, world!");
}
We may build the program:
cargo build 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:
cargo run Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/my_program`
Hello, world!