Hello, World in Rust
Start quickly with cargo, Rust’s build tool and package manager.
Using cargo (recommended)
Create a new binary crate and run it.
cargo new hello
cd hello
cargo run
cargo run compiles and runs src/main.rs in debug mode. For a faster binary, use release:
cargo run --release
main function and println!
The entry point is a main function. Use println! (a macro) for output.
fn main() {
println!("Hello, world!");
}
Format with {} placeholders and arguments.
let name = "Rust";
let version = 1.76;
println!("Hello, {} v{}", name, version);
println!("{name} v{version:.1}"); // named capture (Rust 1.58+)
Debug printing
Use {:?} for debug output of types that implement Debug.
let nums = vec![1, 2, 3];
println!("nums = {:?}", nums);
stderr and eprintln!
Write to stderr with eprintln!.
eprintln!("warning: something happened");
rustc (quick compile)
Compile a single file without Cargo (useful for tiny experiments).
rustc main.rs && ./main
Format and lint
Keep code consistent and idiomatic.
cargo fmt # rustfmt
cargo clippy # lints and suggestions
Comments and docs
// Line comment
/* Block comment */
/// Doc comment for items (appears in rustdoc)
Generate docs: cargo doc --open.
Summary
- Use
cargo newandcargo runto get started println!formats output;{:?}printsDebug- Format with
cargo fmt; lint withcargo clippy