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.)
The error message
Section titled “The error message”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` anywhereWhat it means
Section titled “What it means”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.
Why Rust does this
Section titled “Why Rust does this”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.
How to fix it
Section titled “How to fix it”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 centsaverage: 549 centsAlready 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.
Fix 2: Move main out of the module
Section titled “Fix 2: Move main out of the module”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 definitionsThe 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...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:
cargo new --lib cart # creates src/lib.rs: a library, no main neededcargo new shop # creates src/main.rs with fn main(): a programPut the helpers in src/lib.rs, mark what other code may use with pub, and check them with tests instead of cargo run:
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:
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.
Where you’ll usually see it
Section titled “Where you’ll usually see it”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.
Related errors
Section titled “Related errors”- E0425: cannot find value in this scope: a misspelled name somewhere else in the program, for example calling
totl(). - E0433: failed to resolve:
main.rscan’t find a module or the library crate, for examplecart::totalwith a wrong package name. - E0603: item is private:
mainfound the library function, but it isn’tpub. - E0463: can’t find crate: a crate you
useisn’t inCargo.toml. - All compiler errors explained
Quick check
Section titled “Quick check”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
Read more
Section titled “Read more”- E0601 — Rust error codes index: the official explanation of this error.
- Hello, World! — The Rust Book: your first
mainfunction, and why every program starts there. - Packages and Crates — The Rust Book: binary crates vs library crates,
src/main.rsvssrc/lib.rs. - Cargo Targets — The Cargo Book: libraries, binaries,
src/bin/and how Cargo finds each one. - Main functions — The Rust Reference: the exact rules for
main, including where it may be defined.