Basic Program Structure
Part of the Fundamentals section of Coddy's Rust journey — lesson 4 of 75.
In Rust, every executable program starts with the main function. This function is the entry point of the program, and it's where the execution begins.
The main function is defined using the fn keyword followed by main and a pair of parentheses (). The code to be executed is placed within curly braces {}.
Here's a simple breakdown of a basic Rust program:
fn main() { // Main function
println!("Hello, Coddy!"); // Output statement
}Important note: In Rust, each statement must end with a semicolon (;). The semicolon is mandatory and tells Rust that you've reached the end of a statement.
Forgetting to add a semicolon will result in a compilation error. However, note that code blocks enclosed in curly braces {} (like function declarations) don't need semicolons.
Challenge
BeginnerCreate a Rust program that contains the main function. Inside the main function, write code to output the following text:
This is my first Rust program!Cheat sheet
Every Rust program starts with the main function, which is the entry point where execution begins:
fn main() {
println!("Hello, Coddy!");
}Key points:
- Use
fnkeyword to define functions - Code goes inside curly braces
{} - Each statement must end with a semicolon
; - Use
println!()to output text to the console
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input