Rust

Main Function Wrong Type E0580 Fix

The runtime calls main with no arguments and expects one exact signature: a plain, safe, non-generic Rust function taking no parameters. Give main a parameter - to receive a number, a string, or the command-line arguments directly - and the compiler stops with E0580, naming the signature it expected and the one it found. Some neighboring mistakes look like the same complaint but are not: a wrong return type is E0277, an async main is E0752, and a missing main is E0601. This guide shows each failing shape, which code it actually draws, the declarations that compile and run - including how a Result or ExitCode return turns into a process exit code - and the repairs that give a program its arguments and its exit status without changing main's type.

Written December 2, 2026 · Tested with Rust 1.99.0 · 17 min read

By the end of this guide
  • The rule underneath: main has one exact signature
  • How to read the diagnostic
  • The first failure: the index example
  • Every parameter shape draws the same code

Written December 2, 2026

Tested with Rust 1.99.0

What this guide is

Error E0580 means the main function has the wrong type. The error codes index puts it in one sentence: "The main function was incorrectly declared."

In the shapes probed in this guide, E0580 is drawn by a main that takes a parameter and by a main declared unsafe or with a non-Rust ABI such as extern "C". A wrong return type is a different error - E0277 - even though it is also a main declared with a type the runtime cannot use. This guide shows the E0580 shapes, the neighboring declarations that draw E0277, E0752, E0131, E0646, or E0601 instead, the declarations that compile and run, and the repairs that keep main parameter-free while still reading arguments and choosing an exit code.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts, the file names are the bare names shown on the command line.

The rule underneath: main has one exact signature

The entry point is not called by your code; the runtime calls it, and it calls it in exactly one way - no arguments passed in, as a safe Rust-ABI function. So the declaration has to match the signature the diagnostic prints as expected: fn(). A parameter has nowhere to come from: there is no caller that could supply an i32, a Vec<String>, or a &str. Command-line arguments exist, but they arrive through the standard library - std::env::args - not through the parameter list, which is what the index's own explanation points at, along with std::process::exit for choosing an exit code.

The return side is governed by a different mechanism. main may return a type, but only a type that implements Termination, which is how the returned value becomes a process exit code. That is why a wrong parameter and a wrong return type draw different codes: the parameter breaks the signature itself (E0580), while a return type such as i32 matches the signature's shape and fails only the Termination requirement (E0277).

How to read the diagnostic

For a parameter, the header is "main function has wrong type", the label under the whole declaration says "incorrect number of function parameters", and a note contrasts the two signatures: "expected signature fn()" against, for the index example, "found signature fn(i32)". For an unsafe or extern declaration the label names that exact mismatch instead - "expected safe fn, found unsafe fn" or "expected \"Rust\" fn, found \"C\" fn" - and the note shows the corresponding found signature, such as unsafe fn() or extern "C" fn().

The E0277 diagnostic reads differently: "main has invalid return type i32", with the label "main can only return types that implement Termination" and the help "consider using (), or a Result". If your error names a return type, you are in E0277 territory, not E0580, however similar the underlying mistake feels.

The first failure: the index example

The index's erroneous example, verbatim:

Rust
fn main(x: i32) { // error: main function has wrong type
    println!("{}", x);
}
Output
error[E0580]: `main` function has wrong type
 --> fail_index.rs:1:1
  |
1 | fn main(x: i32) { // error: main function has wrong type
  | ^^^^^^^^^^^^^^^ 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 example is a single i32 parameter, and it draws exactly one E0580 - no second code hides in it. The index gives the corrected form as a parameter-free main (fn main() { // your code }), and points to std::env::args for command-line arguments and std::process::exit for a specified exit code. Both directions are shown running in the repairs below.

Every parameter shape draws the same code

A parameter meant to receive the arguments as a collection fails the same way - the found signature simply names the parameter's type:

Rust
fn main(args: Vec<String>) {
    println!("{:?}", args);
}
Output
error[E0580]: `main` function has wrong type
 --> fail_param_str.rs:1:1
  |
1 | fn main(args: Vec<String>) {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect number of function parameters
  |
  = note: expected signature `fn()`
             found signature `fn(Vec<String>)`

error: aborting due to 1 previous error

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

Two parameters draw E0580 as well, with the found signature fn(i32, i32). A &str parameter draws E0580, with the found signature shown as for<'a> fn(&'a str). A mut parameter - fn main(mut x: i32) - draws E0580 with the found signature fn(i32), alongside a warning that the variable does not need to be mutable. The parameter's type, count, and mutability do not matter in the probed shapes: any parameter at all makes the signature wrong.

Unsafe and extern main: same code, different label

An unsafe declaration takes no parameter and returns nothing, yet still draws E0580, because safety is part of the expected signature:

Rust
unsafe fn main() {
    println!("unsafe");
}
Output
error[E0580]: `main` function has wrong type
 --> fail_unsafe.rs:1:1
  |
1 | unsafe fn main() {
  | ^^^^^^^^^^^^^^^^ expected safe fn, found unsafe fn
  |
  = note: expected signature `fn()`
             found signature `unsafe fn()`

error: aborting due to 1 previous error

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

An extern "C" declaration draws E0580 the same way, with the label "expected \"Rust\" fn, found \"C\" fn" and the found signature extern "C" fn():

Rust
extern "C" fn main() {
    println!("extern");
}
Output
error[E0580]: `main` function has wrong type
 --> fail_extern_c.rs:1:1
  |
1 | extern "C" fn main() {
  | ^^^^^^^^^^^^^^^^^^^^ expected "Rust" fn, found "C" fn
  |
  = note: expected signature `fn()`
             found signature `extern "C" fn()`

error: aborting due to 1 previous error

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

An extern "system" declaration draws E0580 too, with the found signature extern "system" fn(). Combining both qualifiers - unsafe extern "C" fn main() - also draws exactly one E0580; its label reports the safety mismatch first, "expected safe fn, found unsafe fn", and its found signature is unsafe extern "C" fn().

The shapes that are not this error: wrong return types draw E0277

A main returning a type that does not implement Termination does not draw E0580. An i32 return draws E0277:

Rust
fn main() -> i32 {
    0
}
Output
error[E0277]: `main` has invalid return type `i32`
 --> fail_ret_i32.rs:1:14
  |
1 | fn main() -> i32 {
  |              ^^^ `main` can only return types that implement `Termination`
  |
  = help: consider using `()`, or a `Result`

error: aborting due to 1 previous error

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

A String return draws E0277 with the header "main has invalid return type String", and a bool return draws E0277 with the header "main has invalid return type bool". A struct return - a struct Exit(i32) returned from main - draws E0277 with the header "main has invalid return type Exit". An alias does not change the code: type R = i32; with fn main() -> R draws E0277, and the header reports the resolved type, i32.

A declaration wrong on both sides - fn main(x: i32) -> i32 - drew only E0277 in the probed shape, for the return type; no E0580 accompanied it. Fixing only the return type in that shape would leave the parameter for a later compile to reject.

The shapes that are not this error: async, generics, and a where clause

An async main draws E0752, whose header states the rule directly - "main function is not allowed to be async":

Rust
async fn main() {
    println!("async");
}
Output
error[E0752]: `main` function is not allowed to be `async`
 --> fail_async.rs:1:1
  |
1 | async fn main() {
  | ^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

error: aborting due to 1 previous error

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

A generic main - fn main<T>() - draws E0131, "main function is not allowed to have generic parameters". A lifetime parameter draws the same code: fn main<'a>() draws E0131, with the label "main cannot have generic parameters". A where clause draws its own code, E0646 - "main function is not allowed to have a where clause":

Rust
fn main() where i32: Copy {
    println!("where");
}
Output
error[E0646]: `main` function is not allowed to have a `where` clause
 --> fail_where.rs:1:11
  |
1 | fn main() where i32: Copy {
  |           ^^^^^^^^^^^^^^^ `main` cannot have a `where` clause

error: aborting due to 1 previous error

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

The shapes that are not this error: no main at all draws E0601

A crate with no main function at all draws E0601, "main function not found in crate", not E0580. A non-function item named main draws E0601 as well: a crate whose only main is the constant const main: i32 = 5; draws E0601, and the diagnostic notes the "non-function item at crate::main". A struct named main - struct main; - also draws E0601 with the same non-function-item note, alongside a warning that the type name should be upper camel case.

A method named main is not the entry point and is not rejected: a main method taking self inside an impl block, called from a plain crate-level fn main(), compiles and runs, printing self. Only the crate-level main carries the signature rule.

What does work: no return, or an explicit unit return

A plain main with no return annotation compiles and runs, printing its output and exiting with code 0. Writing the unit return explicitly changes nothing: fn main() -> () compiles and runs, printing unit and exiting with code 0. The same holds through an alias: type U = (); with fn main() -> U compiles and runs, printing alias unit and exiting with code 0 - an alias for a permitted return type is permitted, just as an alias for i32 as a return type drew E0277 above.

An empty const fn main() {} also compiles and runs, exiting with code 0 and printing nothing. That probed shape is the empty body only: a const fn main whose body called println! drew E0015, "cannot call non-const function", for the call inside the body - a different code, about the body, not about main's type.

What does work: returning Result, and what the process does with it

A main returning Result<(), std::io::Error> compiles. Returning Ok(()) runs normally - the probed program printed ok path and exited with code 0:

Rust
fn main() -> Result<(), std::io::Error> {
    println!("ok path");
    Ok(())
}
Output
ok path

Returning Err also compiles - the failure appears when the program runs, not when it builds. The probed program, whose main returns an Err holding an io::Error of kind Other with the message boom, printed nothing to stdout, wrote Error: Custom { kind: Other, error: "boom" } to stderr, and exited with code 1.

The ? operator works in a Result-returning main exactly as in any other Result-returning function. With a helper returning Ok(7), the probed main printed got 7 and exited with code 0; with the helper returning an Err, the ? in main ended the program there - nothing printed to stdout, Error: Custom { kind: Other, error: "bad" } went to stderr, and the exit code was 1.

The error type need not be std::io::Error. A main returning Result<(), MyErr>, where MyErr is a local struct implementing Debug, Display, and std::error::Error, compiles: returning Ok(()) printed custom ok and exited with code 0, and returning Err(MyErr) wrote Error: MyErr to stderr and exited with code 1. Even Result<(), ()> compiles: returning Ok(()) exited with code 0, and returning Err(()) wrote Error: () to stderr and exited with code 1.

What does work: ExitCode and a diverging main

A main returning std::process::ExitCode chooses the process exit code directly. The probed program printed exitcode, returned ExitCode::from(3), and the process exited with code 3:

Rust
use std::process::ExitCode;

fn main() -> ExitCode {
    println!("exitcode");
    ExitCode::from(3)
}
Output
exitcode

A diverging main - fn main() -> ! - also compiles, in the probed shape where its body calls std::process::exit(5); the process exited with code 5 and printed nothing.

Entry-point and declaration mistakes cluster together. For a struct literal used on a non-struct type, see Struct Literal on Non-Struct Type E0071 Fix. For a field named twice in one literal, see Field Specified More Than Once E0062 Fix. For type parameters placed on foreign items, see Type Parameters on Foreign Items E0044 Fix.

The repairs

Drop the parameter and read the arguments inside the body. This is the index's corrected form extended with std::env::args, the route the index itself names; run with no arguments, it prints the program name as the first collected argument - the probed run printed the count line below for its own invocation:

Rust
fn main() {
    let args: Vec<String> = std::env::args().collect();
    println!("{} args", args.len());
}
Output
1 args

Return a Result when the program can fail, and let ? carry the error out. The repair from the ? probe above, with a helper that succeeds, prints got 7 and exits with code 0:

Rust
fn helper() -> Result<i32, std::io::Error> { Ok(7) }

fn main() -> Result<(), std::io::Error> {
    let v = helper()?;
    println!("got {}", v);
    Ok(())
}
Output
got 7

Return an ExitCode when the exit status is the point. Returning ExitCode::from(3) from the probed program above made the process exit with code 3 while still printing exitcode first - the status is chosen in the return value, with no parameter and no change to main's parameter list.

Call std::process::exit when a code must be chosen mid-program, the second route the index names. In the diverging probed shape, fn main() -> ! calling std::process::exit(5) exited with code 5.

Fixes in reach order

First, read which code you actually have. If the header is "main function has wrong type" the code is E0580: in the probed shapes that means a parameter on main - of any type, count, or mutability - or an unsafe or non-Rust-ABI (extern "C" or extern "system") declaration. If the header names an invalid return type, the code is E0277; if it says main is not allowed to be async, the code is E0752; a generic or lifetime-parameter main with no parameters draws E0131 in the probed shapes (a lifetime-parameter main that also takes a parameter draws E0580 instead); a where clause draws E0646; and a missing main or a non-function item named main draws E0601.

Second, for E0580, remove every parameter and drop the unsafe or extern qualifier, leaving a plain fn main(). Move whatever the parameter was meant to receive into the body: command-line arguments come from std::env::args.

Third, for E0277, change the return type to one that implements Termination in the probed shapes: () (written explicitly, omitted, or named through an alias for ()), a Result whose error type implements Debug as in the probed std::io::Error, local error struct, and () error shapes, std::process::ExitCode, or the diverging ! in the probed std::process::exit shape.

Fourth, choose the exit behavior deliberately once the declaration compiles: a Result main exits with code 0 on Ok and code 1 on Err, writing Error: followed by the error's Debug form to stderr; an ExitCode main exits with the code it returns; and std::process::exit exits with the code passed to it, from wherever it is called.

Last, leave the legal declarations alone: a plain main, an explicit -> (), an alias for () as the return type, a Result, ExitCode, or diverging main, an empty const fn main() {}, and a method named main on a type all compile in the probed shapes. Do not rewrite them while fixing the E0580 next to them.

Decision summary

E0580 fired for every parameter shape probed - the index example (i32), a Vec<String>, two i32 parameters, a &str, and a mut i32 - and for unsafe fn main(), extern "C" fn main(), extern "system" fn main(), and unsafe extern "C" fn main(), in each case as exactly one error. It did not fire for a wrong return type: i32, String, bool, a struct Exit, and an alias for i32 as the return type all drew E0277, as did a declaration wrong on both sides (fn main(x: i32) -> i32), which drew only E0277. It did not fire for an async main, which drew E0752, for a generic or lifetime-parameter main with no parameters, which drew E0131 in the probed shapes, for a main with a where clause, which drew E0646, or for a missing main, a constant named main, or a struct named main, which drew E0601. The shapes that compiled and ran were a plain main and a main returning () explicitly or through an alias (exit code 0), a Result main with std::io::Error, a local error struct, or () as the error type (exit code 0 on Ok; on Err, exit code 1 with Error: and the error's Debug form on stderr, including through ?), a main returning ExitCode::from(3) (exit code 3), a diverging main calling std::process::exit(5) (exit code 5), an empty const fn main() {} (exit code 0), and a method named main taking self called from a plain main (printing self).

Checklist

  • main takes no parameters - not an integer, a string, a collection of arguments, a reference, or a mut parameter; arguments are read with std::env::args inside the body.
  • main is a plain safe Rust-ABI function: unsafe, extern "C", and extern "system" declarations of main draw E0580.
  • A wrong return type is not E0580: i32, String, bool, a struct, and an alias for i32 as return types draw E0277, because they do not implement Termination.
  • Other declaration mistakes keep their own codes: async is E0752, generic or lifetime parameters on a parameter-free main are E0131 (a lifetime-parameter main that also takes a parameter draws E0580), a where clause is E0646, and no main - or only a constant or struct named main - is E0601.
  • Legal returns in the probed shapes: no annotation, -> () directly or through an alias, a Result (exit 0 on Ok, exit 1 and an Error: line on stderr on Err), ExitCode (exits with the returned code), and ! via std::process::exit (exits with the passed code).
  • A declaration wrong on both sides can report only the return side: the probed parameter-plus-i32-return shape drew only E0277, so recompile after fixing the return type before concluding the parameter is accepted.

Sources

  • Rust error codes index, E0580 - the main function was incorrectly declared; the erroneous example declares fn main(x: i32), the corrected form is a parameter-free fn main(), and the page points to std::env::args for command-line arguments and std::process::exit for a specified exit code.
  • Rust error codes index, E0277 - a trait bound was not satisfied; the code drawn when main returns a type that does not implement Termination, such as i32, String, bool, a struct, or an alias for i32.
  • Rust error codes index, E0752 - the main function is not allowed to be async; the code drawn by async fn main().
  • Rust error codes index, E0131 - the main function is not allowed to have generic parameters; the code drawn by a generic or lifetime-parameter main.
  • Rust error codes index, E0646 - the main function is not allowed to have a where clause; the code drawn by a main with a where clause.
  • Rust error codes index, E0601 - no main function found; the code drawn when a crate has no main function, or only a constant or a struct named main.

Keep Exploring