Menu
Coddy logo textTech

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 icon

Challenge

Beginner

Create 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 fn keyword 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

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals