0
0
Rustprogramming~5 mins

Program structure in Rust

Choose your learning style9 modes available
Introduction

Every Rust program needs a clear structure to work correctly. It helps the computer understand what to do step by step.

When starting a new Rust project to organize your code.
When you want to write a simple program that prints a message.
When you need to define where your program begins running.
When you want to add comments to explain your code.
When you want to separate code into functions for clarity.
Syntax
Rust
fn main() {
    // Your code here
}

fn main() is the starting point of every Rust program.

Curly braces { } contain the code that runs.

Examples
This program prints a greeting message to the screen.
Rust
fn main() {
    println!("Hello, world!");
}
Comments start with // and explain the code but do not run.
Rust
// This is a comment
fn main() {
    // Print a number
    println!("Number: {}", 5);
}
Variables like x store data you can use later.
Rust
fn main() {
    let x = 10;
    println!("Value of x is {}", x);
}
Sample Program

This simple program shows how to write the main function and print text.

Rust
fn main() {
    // Print a welcome message
    println!("Welcome to Rust programming!");
}
OutputSuccess
Important Notes

Every Rust program must have one main function as the entry point.

Use println! macro to print text to the screen.

Comments help others understand your code and do not affect the program.

Summary

Rust programs start running from the main function.

Code inside { } runs step by step.

Use comments to explain your code for yourself and others.