Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Hello, Binary Crate!

Prerequisite: As always, we assume that we are working inside a terminal session running in our usual Docker container (as previously explained in Development Environment Setup).

Source code at rust_playground/rs-ws/hello_world

Here we are, ready to say our “Hello, World!” in Rust. In order to do so, we will create our first binary crate. A binary crate produces an executable program.

We can create our new binary crate inside our Cargo workspace by running the cargo new command:

cd rs-ws
cargo new hello_world --bin

resulting in the creation of a crate having only one source file rust_playground/rs-ws/hello_world/src/main.rs:

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

Then, we can cargo fmt the crate by:

cargo fmt --verbose --package hello_world

We can cargo clippy the crate by:

cargo clippy --verbose --package hello_world

We can cargo check the crate by:

cargo check --package hello_world --release

Or cargo build:

cargo build --package hello_world --release

And finally, cargo run:

cargo run --package hello_world --release

Notice that if we omit the --release flag, the target will be checked/built/run with the default unoptimized + debuginfo profile