src/main.rs in a New Cargo Project
Understand the default main.rs file generated by cargo new and how to run and modify it before adding Axum.
When you run cargo new my-axum-app, Cargo creates a small project skeleton. Inside that skeleton, the most important file for starting your program is src/main.rs. This page explains what this file is, what the default code does, how to run it, and how to start changing it. You do not need to know Rust deeply yet - this page walks through every part of the default file from zero.
The Default Content of src/main.rs
Open the file src/main.rs in any text editor. If you have not changed anything, it contains exactly this:
fn main() {
println!("Hello, world!");
}
That is the complete file. Three lines of Rust. Even with this small amount of code, you have a working program.
What is a source file?:
A source file is a plain text file containing instructions written in a programming language. The Rust compiler reads these files and turns them into a program your computer can run. The main.rs file is the starting point for that process in a binary crate.
What Each Part of the Code Means
The default file has three important parts. Understanding them helps you read and modify Rust later.
The main function
The line fn main() { starts a function named main. A function is a named block of code that does something. In Rust, fn is a keyword that says "a function definition starts here."
The main function is special. When you run a Rust program, the operating system calls this function first. There is no way to start anywhere else. Every binary crate must contain exactly one main function.
The opening curly brace { starts the body of the function. The closing brace } on its own line ends it. Everything between those braces is what the function does when it runs.
The println! macro
Inside main, there is one line:
println!("Hello, world!");
println! prints text to the terminal and moves the cursor to a new line afterward. The ! at the end means println is a macro, not a regular function. A macro is a piece of code that writes more code for you before the compiler checks it. For now, you can treat println! as a built‑in way to display something on the screen.
Inside the parentheses, you place a string literal - text wrapped in double quotes. The string "Hello, world!" is exactly what gets printed.
The semicolon
The semicolon ; at the end of the line tells Rust that this statement is finished. In Rust, most statements end with a semicolon. Forgetting it is one of the most common beginner mistakes, and the compiler will give you an error message that might look confusing at first.
Semicolons are required after most statements:
If you write println!("Hello, world!") without the semicolon and another statement follows it, the compiler reports error: expected ';' and points at the next line. The fix is simple: add the semicolon back.
Running the Program
Before changing anything, run the program to see what it does. Make sure your terminal is in the project directory (the folder that contains Cargo.toml). Then type:
cargo run
Cargo compiles your source file and then launches the resulting program. The first time you run this, compilation may take a few seconds. The standard library is precompiled and ships with your Rust toolchain, so there is nothing extra to download for a project with no dependencies. After that, runs are fast.
You should see output like this:
Compiling my-axum-app v0.1.0 (/path/to/my-axum-app)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.35s
Running `target/debug/my-axum-app`
Hello, world!
The last line, Hello, world!, comes from your program. The lines above it are Cargo telling you what it did to build and run the program.
Your first Rust program is working:
Seeing Hello, world! printed means your Rust toolchain, Cargo, and the generated project are all working correctly. This is the same check developers do when they start a new project.
Changing the Printed Message
The easiest way to modify the program is to change the text inside the quotes. Replace "Hello, world!" with something else:
// src/main.rs
fn main() {
println!("I am learning Axum!");
}
Save the file and run cargo run again. You will see:
I am learning Axum!
You can change the string to anything you want, as long as it stays inside double quotes.
Printing Multiple Lines
You are not limited to one println!. Add several of them, each on its own line:
// src/main.rs
fn main() {
println!("First line");
println!("Second line");
println!("Third line");
}
Run it. The output appears on three separate lines:
First line
Second line
Third line
Each println! sends its text and then moves to the next line. That is why they do not run together.
Using Variables to Hold Text
A variable is a named storage spot for a value. You can store a string in a variable and then print that variable. This is useful when the same text appears more than once or when you want to change the value in one place later.
// src/main.rs
fn main() {
let message = "Hello from a variable!";
println!("{}", message);
}
The let keyword creates a variable. The name is message, and the value is the string on the right side. In the println! line, the {} is a placeholder that gets replaced by the value of message. The variable name appears after the comma.
Run this. The output is:
Hello from a variable!
This pattern - storing a value and then printing it with {} - is used constantly in Rust and in Axum applications later.
Combining Multiple Values in One Print
You can print more than one variable at a time. Each {} placeholder matches the corresponding value in the list after the comma.
// src/main.rs
fn main() {
let project_name = "my-axum-app";
let year = 2026;
println!("Project: {} started in {}", project_name, year);
}
Output:
Project: my-axum-app started in 2026
The first {} gets project_name, the second gets year. Rust knows how to display both a string and a number because println! can format many common types.
Strings and numbers are different types:
In Rust, "my-axum-app" is a string of characters, while 2026 is an integer (a whole number). They are stored differently inside the computer. println! can print both, but later you will see that you cannot always mix them freely without converting types.
What Happens If You Break the File
Making mistakes is part of learning. Here are three common errors and the messages you might see.
Missing a semicolon
If you delete the semicolon from a println! that is followed by another statement:
fn main() {
println!("Hello")
println!("World!");
}
Cargo will refuse to run and show an error like:
error: expected `;`, found `println`
This means the compiler reached the next statement before it found the expected semicolon. The fix is to add ; at the end of the first println! line.
There is one subtlety worth knowing: if the semicolon-less println! is the last line of main, the code actually compiles - a macro call without a semicolon is a valid tail expression that returns (), which is exactly what main returns. The error only appears when another statement follows it.
Writing main with a capital letter
If you write:
fn Main() {
println!("Hello, world!");
}
The compiler reports:
error[E0601]: `main` function not found in crate `my_axum_app`
Rust looks for a function named main (all lowercase). Main is a different name, so the program has no starting point. Change it back to main.
Using single quotes for strings
If you write:
fn main() {
println!('Hello, world!');
}
The compiler reports error[E0762]: unterminated character literal. Rust treats single quotes as a character, which can hold only one letter. Double quotes are for strings of any length. Replace single quotes with double quotes.
A missing main function prevents the program from starting:
The main function is not optional. Without it, Rust has no idea where to begin executing your code. Every binary project you create with Cargo includes this function for a reason. If you accidentally rename or delete it, the program will not compile.
Where src/main.rs Fits in the Axum Project
You created this file as part of a new Cargo project. The next steps in building an Axum web application involve replacing the simple Hello, world! code with Axum-specific code. That code will still live inside src/main.rs, but it will use crates like axum and tokio to start a web server instead of just printing a line.
For now, the important thing is that you understand this file is the entry point. Everything else you add to the project will eventually be reachable from here. You will modify this exact file many times as you build the server.
Summary
The single insight from this page is that a whole Rust program is just a main function that the operating system calls first. The three lines in src/main.rs - a function, a print, a semicolon - are the smallest version of that contract, and the same contract holds when the file grows into a web server: main still runs first, and everything else is code that main reaches. The practical payoff is a working check you can repeat any time: if cargo run prints what you expect, your toolchain and your project layout are both correct, and any problem after that lives in the code you just changed, not in the setup.