BeginnerLesson 1 of 16
Hello World, println! & Cargo – Rust
Your first Rust program, output macros, and the Cargo workflow.
Every executable Rust program starts in the function main. println! prints a line. The exclamation mark shows that it is a macro, not a regular function.
fn main() {
println!("Hello World!");
println!("I am learning {}", "Rust");
println!("Number: {}", 42);
}The placeholder {} uses the Display trait, and {:?} uses the Debug trait (good for debugging lists and structs). Since Rust 1.58 you can name variables directly in the format string; with extra specifiers you control width and precision.
fn main() {
let name = "Ada";
let alter = 36;
println!("{name} is {alter} years old");
println!("{0} {1} {0}", "a", "b"); // a b a
println!("{:>6.2}|{:<5}|{:05}", 3.14159, "ab", 42); // 3.14|ab |00042
println!("{:?}", vec![1, 2, 3]); // [1, 2, 3]
}Comments: // for a single line, /* ... */ for blocks, and /// for documentation comments, from which cargo doc generates a web page. Day-to-day work runs through Cargo:
cargo new hallo # new project (folder hallo/)
cd hallo
cargo run # build and run (debug build)
cargo check # check only, very fast
cargo build --release # optimized build -> target/release/TipDebug builds compile quickly but run much slower. Always do performance measurements with
--release.Warning
println!("{}", meine_liste) does not work because Vec has no Display. Use {:?} or {:#?} (pretty-printed).