Beginner Fundamentals
Syntax
Rust syntax is concise and explicit. Code lives inside functions, blocks use braces, and most statements end with a semicolon.
Functions and braces
A function is declared with fn. The body goes between curly braces { }.
fn main() {
println!("Learning Rust syntax");
}
Semicolons
A statement ends with a semicolon ;. The compiler treats lines without a trailing semicolon as expressions, which matters for return values.
fn main() {
let x = 5; // statement, ends with ;
println!("{}", x); // statement, ends with ;
}
Macros with !
A name followed by ! is a macro, not a function. Macros generate code at compile time and can do things regular functions cannot, like accepting a variable number of arguments.
fn main() {
println!("A macro"); // prints with a newline
print!("No newline "); // prints without a newline
eprintln!("Error output"); // prints to standard error
}
The {} inside the string is a placeholder filled by the arguments that follow.