Basics
Hello
Values and Output
A Rust program runs from main. This example binds a name, builds a greeting with format!, and prints it.
Program
Play the program to watch name flow into greeting, then print.
hello.rs
Replay: real traced execution (multi-file project)
fn main() {
let name = "Ada";
let greeting = format!("Hello, {name}!");
println!("{greeting}");
}
name ← "Ada", greeting ← "Hello, Ada!"
1fn main() {2 let nam→ "Ada"e = "Ada";3 let greetin→ "Hello, Ada!"g = format!("Hello, {name}!");4 println!("{greeting}");5}outputHello, Ada!
Follow the Greeting
namestarts as"Ada".format!("Hello, {name}!")putsAdainto the greeting text.greetingbecomesHello, Ada!.println!prints that greeting on one line. | value name | value | | --- | --- | |name| Ada | |greeting| Hello, Ada! | | printed output | Hello, Ada! |
let binding
`let` binds a value to an immutable name.
format!
`format!` builds a `String` from a template and values.
println!
`println!` writes a line to standard output.
Exercise: hello.rs
Reproduce the output Hello, Ada!, then change name and predict the exact greeting before running it.