Skip to content

Rust Error E0601: `main` Function Not Found in Crate

E0601 means: Rust wanted to build a program, but couldn’t find where it starts. Every Rust program begins in a function called exactly main, written at the top level of src/main.rs. If it’s missing, misspelled, or tucked inside a module, you get this error. It’s very common when you copy a snippet from a tutorial or a library’s docs, and it’s always quick to fix. (This page is part of Rust compiler errors explained.)

error[E0601]: `main` function not found in crate `main`
--> src/main.rs:7:2
|
7 | } // ❌ end of the file, and no `fn main` anywhere
| ^ consider adding a `main` function to `src/main.rs`

This is the program that produces it:

fn total(prices: &[u32]) -> u32 {
prices.iter().sum()
}
fn average(prices: &[u32]) -> u32 {
total(prices) / prices.len() as u32
} // ❌ end of the file, and no `fn main` anywhere

The ^ points at the very end of the file, just after the last }. That looks odd, but it makes sense: rustc read the whole file, looking for fn main, and reached the end without finding one. There’s no wrong line to point at; the problem is a line that isn’t there.

The crate name in the message depends on how you build. This output comes from rustc src/main.rs, so the crate is called main, after the file. With cargo run, you’ll see your package name from Cargo.toml instead, for example `main` function not found in crate `shop`. Same error, same fixes.

It’s like a play script with great scenes but no “Act 1, Scene 1”. The actors are ready, but nobody knows where the show begins.

A Rust binary (a program you run) needs one fixed entry point. When you start the program, the operating system calls main, and everything else happens because main calls it. Rust doesn’t guess: not the first function in the file, not a function called start, not Main. The name must be exactly main, all lowercase, at the top level of the crate.

Code that isn’t a program on its own, like a set of helpers that other code uses, is a library. A library has no main and is written in src/lib.rs instead. E0601 often means the code is fine, but it’s in the wrong kind of crate. See Binary and library crates together and Get started.

Fix 1: Add a main function (or fix its spelling)

Section titled “Fix 1: Add a main function (or fix its spelling)”

Choose this most of the time: you copied functions from somewhere, or you started typing helpers and haven’t written main yet. Add fn main() and call your code from it:

fn total(prices: &[u32]) -> u32 {
prices.iter().sum()
}
fn average(prices: &[u32]) -> u32 {
total(prices) / prices.len() as u32
}
fn main() { // ✅ the program starts here
let prices = [250, 199, 1200];
println!("total: {} cents", total(&prices));
println!("average: {} cents", average(&prices));
}

Output:

total: 1649 cents
average: 549 cents
▶ Try it Yourself

Already have a main? Then check its spelling. fn Main(), fn mian() or fn main_() all give exactly the same E0601, because to Rust they are just other functions. The name is case-sensitive, and it must be a function: fn main(), with the parentheses.

Choose this when the message says the main function must be defined at the crate level. You have a main, but it’s inside a mod, so it’s really app::main, not the program’s entry point:

error[E0601]: `main` function not found in crate `main`
--> src/main.rs:5:2
|
5 | }
| ^ the main function must be defined at the crate level (in `src/main.rs`)
|
note: here is a function named `main`
--> src/main.rs:2:5
|
2 | pub fn main() { // ❌ inside a module, not at the top level
| ^^^^^^^^^^^^^
= note: you have one or more functions named `main` not defined at the crate level
= help: consider moving the `main` function definitions

The program behind it:

mod app {
pub fn main() { // ❌ inside a module, not at the top level
println!("Starting the shop...");
}
}

Put the real main at the top level. Code in the module can stay there; give it a clearer name like run and call it from main:

mod app {
pub fn run() {
println!("Starting the shop...");
}
}
fn main() { // ✅ at the top level of src/main.rs
app::run();
}

Output:

Starting the shop...
▶ Try it Yourself

The same applies to modules in other files: a pub fn main() in src/app.rs is app::main, never the entry point. See Modules.

Fix 3: Make it a library, if it isn’t meant to run on its own

Section titled “Fix 3: Make it a library, if it isn’t meant to run on its own”

Choose this when the code is a set of helpers that other code uses, with nothing to “run”. Libraries don’t have a main. Create the project with --lib:

Terminal window
cargo new --lib cart # creates src/lib.rs: a library, no main needed
cargo new shop # creates src/main.rs with fn main(): a program

Put the helpers in src/lib.rs, mark what other code may use with pub, and check them with tests instead of cargo run:

src/lib.rs
pub fn total(prices: &[u32]) -> u32 {
prices.iter().sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn adds_prices() {
assert_eq!(total(&[3, 4]), 7);
}
}

Run cargo build or cargo test on a library. cargo run doesn’t work there; it says error: a bin target must be available for `cargo run`, which is Cargo’s way of saying “there’s no program in this package”. If you want both, add a src/main.rs next to src/lib.rs. It uses the library by the package name:

src/main.rs
fn main() {
let sum = cart::total(&[250, 199, 1200]);
println!("total: {} cents", sum);
}

Now cargo run prints total: 1649 cents. See Binary and library crates together and Testing.

Pasting a snippet from docs or a tutorial. Many examples show only a function or a struct, not a whole program. Pasted into src/main.rs, they give E0601. Fix: wrap the lines that do something in fn main() { ... }, and leave functions and structs outside it. (The Rust Playground notices a missing main and offers to build the code instead of running it, so there you usually won’t see this error.)

Your editor marks the last line of main.rs red. rust-analyzer runs cargo check in the background, so you see E0601 on the last line while you’re still writing helpers. That’s fine: the mark goes away as soon as you add fn main().

Extra programs in src/bin/. Every file in src/bin/, like src/bin/import.rs, is a separate program, and each one needs its own fn main(). Fix: add main to that file, or move shared code into src/lib.rs. See the Cargo targets link below.

Quick check

1. Your src/main.rs has fn Main() { ... }. What does cargo run say?

2. You're writing helpers for other code to use, with nothing to run. What's the right setup?

3. rustc says "the main function must be defined at the crate level". What's wrong?

Score: 0 / 3