Concept Flow - Writing first Rust program
Start program
Call main function
Execute println! macro
Print message to screen
End program
The program starts by calling the main function, which runs the println! macro to print a message, then ends.
fn main() {
println!("Hello, world!");
}| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Start program | Program begins | Ready to run main() |
| 2 | Call main function | Enter main() | Inside main() |
| 3 | Execute println! macro | Print string | Displays: Hello, world! |
| 4 | End main function | main() finishes | Program ends |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| No variables | N/A | N/A | N/A | N/A |
fn main() { ... } defines the program start.
Use println!("text") to print messages.
Program runs main(), executes println!, then ends.
println! is a macro for printing.
No variables needed for this simple program.main function.fn main().fn main() function -> Option Aprintln! to print text with a newline.println!("Hello, world!"); is valid Rust syntax for printing with a newline.fn main() {
println!("{}", 5 + 3);
}{} inside the string is a placeholder for the value following the string.5 + 3 evaluates to 8, which replaces the {}.fn main() {
println!("Hello, world!")
}println!("Hello, world!") line is missing a semicolon at the end.{} as a placeholder and variables are passed after the string.println!("Hello, {}!", name); to print the variable's value.Pass the test and this pattern moves unknown