Rust

E0601 No Main Function Found Fix

A binary crate starts at a function named exactly `main`, defined at the top level of the crate root. If no such function exists - because the file has only helpers, is empty, mistypes the name, or hides main inside a module - there is nothing to run, and the crate does not compile.

Written November 28, 2026 · Tested with Rust 1.99.1 · 12 min read

By the end of this guide
  • The rule underneath: a binary crate needs a main at the crate root
  • The first failure: a file with only a helper function
  • The second failure: an empty file
  • The third failure: the entry point mistyped

Written November 28, 2026

Tested with Rust 1.99.1

What this guide is

Error E0601 fires when the compiler builds a binary crate and finds no main function in it. The error is not about any line of code being wrong - every program in the failure sections below is valid Rust as far as its own functions go. It is about the crate as a whole: a binary is a program, a program needs a starting point, and the starting point is a function named main defined at the top level of the crate root file. Without one, there is nothing to run, so there is nothing to build.

This guide reproduces the error in four shapes - a file with only a helper function, an empty file, a file whose entry point is mistyped, and a file whose only main sits inside a module - and then gives the two repairs: add a top-level fn main(), or, when the file was never meant to be a program, compile it as a library crate instead. The last section separates the case that sounds closest but carries a different code: a main that exists but is declared with the wrong signature is E0580.

The rule underneath: a binary crate needs a main at the crate root

The Rust Reference's crates and source files chapter puts the pieces together. Each compilation processes a single crate and produces either an executable or some sort of library. A crate that contains a main function can be compiled to an executable, and if a main function is present, it must take no arguments. The official E0601 error page states the error from the other side: no main function was found in a binary crate, and to fix it, add a main function.

Three details of that rule explain every shape below. The name is exact: the compiler looks for main, and a function named anything else, however close, does not count. The position is exact: main belongs to the crate root, the top level of the file the compiler was given, not inside a mod. And the requirement belongs to binary crates: a library crate produces no executable and starts nowhere, so it needs no main at all.

The first failure: a file with only a helper function

A file can hold perfectly good code and still not be a program:

Rust
fn helper() -> i32 {
    42
}
Terminal
$ rustc --edition 2021 p1_fail_no_main.rs -o p1_fail_no_main.bin
error[E0601]: `main` function not found in crate `p1_fail_no_main`
 --> p1_fail_no_main.rs:3:2
  |
3 | }
  |  ^ consider adding a `main` function to `p1_fail_no_main.rs`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0601`.

There is no faulty line to point at, so the span lands at the end of the file, on the closing brace of the only function there is, and its label carries the whole diagnosis: consider adding a main function to this file. The crate is named after the file, p1_fail_no_main, and the header names what is missing from that crate. helper itself is never criticized - it is simply not an entry point, and nothing in the file is.

The second failure: an empty file

The limiting case is a file with nothing in it at all. This program is an empty file, zero bytes:

Rust
Terminal
$ rustc --edition 2021 p2_fail_empty.rs -o p2_fail_empty.bin
error[E0601]: `main` function not found in crate `p2_fail_empty`
  |
  = note: consider adding a `main` function to `p2_fail_empty.rs`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0601`.

Same header, different presentation: with no code anywhere, there is no position to underline, so the diagnostic drops the span entirely and the same advice arrives as a note instead of a span label. The two forms are one error - whether the suggestion is printed under a caret or after = note: depends only on whether the file gives the compiler a place to point. A new, still-empty source file compiled as a binary fails for exactly this reason, before a single line is written.

The third failure: the entry point mistyped

A near miss on the name counts as no main at all:

Rust
fn mian() {
    println!("hi");
}
Terminal
$ rustc --edition 2021 p3_fail_typo.rs -o p3_fail_typo.bin
error[E0601]: `main` function not found in crate `p3_fail_typo`
 --> p3_fail_typo.rs:3:2
  |
3 | }
  |  ^ consider adding a `main` function to `p3_fail_typo.rs`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0601`.

The diagnostic is identical in shape to the first failure, and notably it does not suggest the typo: there is no "did you mean mian" help, because the compiler is not matching the name approximately - it is looking for a function with exactly the name main at the crate root, and mian is just one more function that is not it. When a program that looks complete fails with E0601, read the entry point's name character by character before looking anywhere else; the fix here is renaming mian to main, and the corrected program is the first fix below with the name spelled right.

The fourth failure: main defined inside a module

The name can be exactly right and the position still wrong. A main nested in a mod belongs to that module, not to the crate root:

Rust
mod inner {
    pub fn main() {
        println!("hi");
    }
}
Terminal
$ rustc --edition 2021 p6_fail_nested.rs -o p6_fail_nested.bin
error[E0601]: `main` function not found in crate `p6_fail_nested`
 --> p6_fail_nested.rs:5:2
  |
5 | }
  |  ^ the main function must be defined at the crate level (in `p6_fail_nested.rs`)
  |
note: here is a function named `main`
 --> p6_fail_nested.rs:2:5
  |
2 |     pub fn main() {
  |     ^^^^^^^^^^^^^
  = note: you have one or more functions named `main` not defined at the crate level
  = help: consider moving the `main` function definitions

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0601`.

This is the one failure shape where the compiler can see a main somewhere, and the diagnostic changes to say so: the end-of-file label now reads "the main function must be defined at the crate level", a note points at the nested function itself, and the help says to move the definition. pub does not rescue it - visibility is not the issue, level is. Moving that function out of mod inner to the top level of the file is the whole fix.

The first fix: add a top-level fn main()

The direct repair adds the missing entry point at the top level of the crate root, alongside the code that was already there:

Rust
fn helper() -> i32 {
    42
}

fn main() {
    println!("helper {}", helper());
}
Terminal
$ rustc --edition 2021 p4_fix_main.rs -o p4_fix_main.bin
$ ./p4_fix_main.bin
helper 42

Nothing about helper changed. The crate now has a starting point, main calls the helper, and the value it returns is what the program prints. This is the right fix whenever the file is meant to be run: a script-style single file, the root of a binary crate, an example. The added main takes no arguments and returns nothing, which is the plain form of the entry point; a main declared with arguments is a different error, covered in the neighboring section below.

The second fix: compile the file as a library crate

Sometimes E0601 is the compiler reporting a mismatch of intent: the file was written as a library - a set of functions for other code to call - and is being compiled as if it were a program. A library crate needs no main, because it is never run directly. Compiling the helper file with --crate-type lib builds it as one:

Rust
pub fn helper() -> i32 {
    42
}
Command
$ rustc --edition 2021 --crate-type lib p5_fix_library.rs -o p5_fix_library.rlib

The compile is silent and produces p5_fix_library.rlib, a library another crate can link against. Two details carry the change. The flag selects the kind of crate, and with no executable to produce, the missing entry point is no longer missing - it is not required. And helper is written pub here: a non-public function in a library crate is unreachable from outside and draws a dead-code warning, while pub makes it part of what the library offers. Choose this fix when nothing in the file was ever meant to run on its own; choose the first fix when it was.

Neighboring error: a main with the wrong signature is E0580

A main that exists but is declared wrong does not raise E0601 - the function was found, and then rejected. A main that takes an argument is E0580:

Rust
fn main(_extra: i32) {
    println!("hi");
}
Terminal
$ rustc --edition 2021 p7_neighbour_wrong_type.rs -o p7_neighbour_wrong_type.bin
error[E0580]: `main` function has wrong type
 --> p7_neighbour_wrong_type.rs:1:1
  |
1 | fn main(_extra: i32) {
  | ^^^^^^^^^^^^^^^^^^^^ incorrect number of function parameters
  |
  = note: expected signature `fn()`
             found signature `fn(i32)`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0580`.

The contrast is the cleanest way to read an E0601 header. E0580 points at the main declaration itself, underlines it, and states the expected signature against the found one - there is a main, and its shape is the problem, fixed by removing the parameter. E0601 points at the end of the file, or nowhere, because there is no declaration to point at. The parameter here is named _extra so the parameter is the only fault in the program: written as plain extra, the same program would also draw an unused-variable warning on top of the error, which is noise next to the point - the signature alone is what E0580 is about. Command-line arguments are not a reason to give main parameters; a program that needs them reads them from the environment inside a parameterless main, as the E0580 error page directs.

Decision summary

  • The header says "main function not found in crate" (E0601), with the span at the end of the file: the crate root has functions but none of them is a top-level main - add one, as in the first fix.
  • The header says E0601 with no span and the advice as a note: the file is empty - write the program, starting with fn main(), or stop compiling the empty file as a binary.
  • The program looks complete but fails with E0601: read the entry point's name exactly - a function named mian, or anything other than main, does not count, and the compiler will not suggest the correction.
  • The notes say a function named main is "not defined at the crate level": the main is inside a mod - move it to the top level of the crate root file.
  • The file is a set of functions meant for other code to call, and nothing in it should run directly: it is a library - compile it with --crate-type lib and make the offered functions pub.
  • The header instead says "main function has wrong type" (E0580), pointing at the main declaration: a main exists but its signature is wrong - remove its parameters so it matches fn().

Checklist

  • The crate root file defines a function named exactly main, spelled character for character, at the top level of the file - not inside a mod, impl, or function.
  • That main takes no arguments; arguments a program needs are read inside it, not declared on it.
  • A file with no entry point is compiled as a binary only on purpose; a file meant as a library is compiled with --crate-type lib and its offered functions are pub.
  • An E0601 with no span means the file is empty; an E0601 whose notes mention a main not at the crate level means the function exists but is nested.
  • The fixed binary compiles, runs, and prints what its main was written to print; the fixed library compiles to an .rlib with no error.
  • E0463 Can't Find Crate Fix - the crate-level sibling on the import side: a crate is declared but cannot be found, where here the crate is present and its entry point is what is missing.
  • E0583 File Not Found for Module Fix - the other not-found error at the top of a crate: a module is declared but its file is missing, fixed by putting the file where the declaration looks for it.
  • E0432 Unresolved Import Fix - the import-side resolution failure: a use names something the compiler cannot resolve, where E0601 is the one name - main - that a binary crate itself cannot do without.

Sources

  • Rust, Error codes index: E0601 - the definition of the error: no main function was found in a binary crate; its stated fix is to add a main function, shown as a top-level fn main() that prints a greeting - the requirement and the first fix in this guide.
  • The Rust Reference, Crates and source files - the crate model underneath: each compilation processes a single crate and produces either an executable or some sort of library; a crate that contains a main function can be compiled to an executable, and if a main function is present, it must take no arguments - the binary-versus-library split the two fixes rest on.
  • Rust, Error codes index: E0580 - the neighboring error: the main function was incorrectly declared, its example is a main taking an x: i32 argument, the prototype should never take arguments, and command-line arguments are obtained with std::env::args instead - the boundary the neighboring section draws.

Keep Exploring