Hello World
fn main() {
println!("Hello, world!");
}
Run the program:
cargo run
This is the normal Rust entry point. println! is a macro that writes formatted output to the terminal.
Variables
Variables are immutable by default. Use mut when the value should change.
let language = "Rust";
let mut count = 1;
count += 1;
Rust values are immutable unless you mark them with mut, which makes state changes explicit.
Ownership
Values have a single owner. Borrow with references when a function should read data without taking ownership.
fn main() {
let name = String::from("Dan");
greet(&name);
println!("{}", name);
}
fn greet(name: &str) {
println!("Hello, {name}");
}
This shows borrowing with &name, so the function can read the value without taking it away from the caller.