Rust

Main Function Type Parameters E0131 Fix

The entry point has no caller in Rust code to choose its type arguments, so main cannot be generic. Any angle brackets after fn main - a type parameter, a bound, a const generic, or a lifetime - draw E0131. The fix is to keep main plain and move the generic work into a helper function that main calls with concrete types. This guide shows each generic shape failing, the nearby main signatures that compile, the neighboring signature errors with different codes, and the helper repair running.

Written December 1, 2026 · Tested with Rust 1.99.0 · 12 min read

By the end of this guide
  • The rule underneath: the runtime calls main, not Rust code
  • How to read the diagnostic
  • The first failure: the index example
  • Every kind of parameter draws the same code

Written December 1, 2026

Tested with Rust 1.99.0

What this guide is

Error E0131 means the main function was defined with generic parameters. The error codes index puts it in one line: "The main function was defined with generic parameters", gives the erroneous example fn main<T>(), and adds: "It is not possible to define the main function with generic parameters. It must not take any arguments."

That one example covers a wider family than it shows. A bound on the parameter, a second parameter, a where clause, a const generic, and a lifetime parameter all draw the same code, with the span covering exactly the angle-bracket list that was written. But neighboring mistakes around main do not all draw E0131: a generic main that also takes a value argument draws E0580, an async main draws E0752, a main with a value argument draws E0580, and a main returning i32 draws E0277. And the work a generic main seemed to want is legal one function away: a generic helper called from a plain main, a main returning Result or ExitCode, and a main reading its arguments through std::env::args all compile and run.

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

The rule underneath: the runtime calls main, not Rust code

A generic function is normally called by Rust code that supplies - or lets the compiler infer - its type arguments. main has no such caller: the runtime starts the program by calling main with a fixed, known signature, as the Rust Reference entry point describes. There is nobody to pick T, nobody to supply a const value, and no Rust type for a lifetime on main to refer to. So the compiler rejects any generic parameter list on main outright, before the body is considered.

That is why the repair is structural, not a tweak to the parameters. Removing a bound, renaming the parameter, or moving it into a where clause does not help, because the defect is the existence of the list, not its contents. The generic work has to live in a different function, where a real caller - main itself - can supply concrete types.

How to read the diagnostic

The header is always the same sentence: "main function is not allowed to have generic parameters". The span underlines the whole parameter list between the angle brackets - <T>, <T: Clone>, <const N: usize>, <'a> - and the note on the span repeats the rule: "main cannot have generic parameters". There is no help line suggesting a replacement signature; the only fix the diagnostic leaves open is removing the list, which is the repair shown at the end of this guide.

One precedence matters when reading a broken main: if main is generic and also takes a value argument, the compiler reports the argument problem first, as E0580, and E0131 does not appear at all in that program. Fix the argument and the generic list is reported next. The section on that shape below shows the exact transcript.

The first failure: the index example

The index's erroneous example, verbatim apart from the comment:

Rust
fn main<T>() {
}
Output
error[E0131]: `main` function is not allowed to have generic parameters
 --> fail_index.rs:1:8
  |
1 | fn main<T>() {
  |        ^^^ `main` cannot have generic parameters

error: aborting due to 1 previous error

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

Adding a body changes nothing - the same program with println!("hi"); inside draws the identical error at the identical span. The body is never the problem; the <T> is.

Every kind of parameter draws the same code

A bound does not save the parameter. The span simply widens to cover the bound:

Rust
fn main<T: Clone>() {
    println!("hi");
}
Output
error[E0131]: `main` function is not allowed to have generic parameters
 --> fail_bounds.rs:1:8
  |
1 | fn main<T: Clone>() {
  |        ^^^^^^^^^^ `main` cannot have generic parameters

error: aborting due to 1 previous error

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

Two parameters draw one error, not two:

Rust
fn main<T, U>() {
    println!("hi");
}
Output
error[E0131]: `main` function is not allowed to have generic parameters
 --> fail_two.rs:1:8
  |
1 | fn main<T, U>() {
  |        ^^^^^^ `main` cannot have generic parameters

error: aborting due to 1 previous error

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

A where clause does not move the problem out of the way either - the parameter is still declared in the angle brackets, and that list is what is underlined:

Rust
fn main<T>() where T: Clone {
    println!("hi");
}
Output
error[E0131]: `main` function is not allowed to have generic parameters
 --> fail_where.rs:1:8
  |
1 | fn main<T>() where T: Clone {
  |        ^^^ `main` cannot have generic parameters

error: aborting due to 1 previous error

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

Const generics are generic parameters too:

Rust
fn main<const N: usize>() {
    println!("hi");
}
Output
error[E0131]: `main` function is not allowed to have generic parameters
 --> fail_const_generic.rs:1:8
  |
1 | fn main<const N: usize>() {
  |        ^^^^^^^^^^^^^^^^ `main` cannot have generic parameters

error: aborting due to 1 previous error

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

And so are lifetimes:

Rust
fn main<'a>() {
    println!("hi");
}
Output
error[E0131]: `main` function is not allowed to have generic parameters
 --> fail_lifetime.rs:1:8
  |
1 | fn main<'a>() {
  |        ^^^^ `main` cannot have generic parameters

error: aborting due to 1 previous error

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

Type, const, and lifetime parameters are the three kinds Rust has, and all three are rejected on main with the same header. There is no fourth spelling to try.

The shape that is not this error, and hides it: generic main with an argument

Give a generic main a value argument as well, and E0131 disappears - replaced by a different code:

Rust
fn main<T>(x: T) {
    println!("hi");
}
Output
error[E0580]: `main` function has wrong type
 --> fail_generic_and_args.rs:1:1
  |
1 | fn main<T>(x: T) {
  | ^^^^^^^^^^^^^^^^ incorrect number of function parameters
  |
  = note: expected signature `fn()`
             found signature `fn(T)`

warning: unused variable: `x`
 --> fail_generic_and_args.rs:1:12
  |
1 | fn main<T>(x: T) {
  |            ^ help: if this is intentional, prefix it with an underscore: `_x`
  |
  = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

error: aborting due to 1 previous error; 1 warning emitted

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

The compiler reports the wrong-type problem, E0580, and never mentions the generic list. This does not mean the generic list became legal: remove the argument and the same <T> draws E0131, as shown above. When a broken main shows E0580, fix the signature it names first, then expect E0131 if angle brackets remain.

What does work: a generic helper called from main

Generics are legal everywhere except on main itself. The standard repair moves the generic work into a helper, and main calls it with concrete types:

Rust
fn run<T: std::fmt::Display>(value: T) {
    println!("got {}", value);
}
fn main() {
    run(42);
    run("hello");
}
Output
got 42
got hello

The output is got 42 then got hello: the helper is instantiated twice, once for the integer and once for the string, and both calls are chosen by main, the real caller the generic needed. If the type genuinely varies per run of the program - for example by a command-line choice - branch in main and call the helper with the concrete type for that branch.

Where no generic is actually needed, a concrete helper is simpler still: fn run(value: i32) called as run(42) from a plain main compiles and prints got 42. And a plain main on its own, fn main() { println!("plain main"); }, compiles and prints plain main.

What does work: main returning Result or ExitCode

A return type is not a generic parameter, and the two return types designed for main compile and run. A main returning a Result propagates errors with ? in real programs; in its smallest form it prints and returns Ok:

Rust
fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("ok result");
    Ok(())
}
Output
ok result

A main returning ExitCode reports a process status without panicking:

Rust
use std::process::ExitCode;
fn main() -> ExitCode {
    println!("ok exitcode");
    ExitCode::SUCCESS
}
Output
ok exitcode

Both programs exit successfully. Neither needs angle brackets on main: the error and status types live in the return position, where the runtime's fixed contract already allows them.

What does work: arguments through std::env::args

The index's second sentence - "It must not take any arguments" - means main takes no parameters in its signature. Program arguments are read from inside the body instead:

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

Run with no extra arguments, the output is argc 1: the one entry is the program's own name. Writing the arguments as a parameter instead is not a shortcut around this - it is E0580, in the neighboring-codes section below.

Neighboring codes: E0580, E0752, E0277, E0601

Four mistakes around main sit next to E0131 and are routinely confused with it. Each keeps its own code.

A value argument on main - E0580. Both fn main(args: Vec<String>) and fn main(x: i32) draw E0580, "main function has wrong type", with the note "expected signature fn()" against the signature found. For the Vec<String> form the transcript is:

Rust
fn main(args: Vec<String>) {
    println!("{:?}", args);
}
Output
error[E0580]: `main` function has wrong type
 --> fail_main_args.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`.

Read arguments with std::env::args, as shown above, instead of declaring them.

An async main - E0752. Writing async fn main() without a runtime attribute draws E0752, "main function is not allowed to be async", underlining the whole function. Async entry points need a runtime's attribute macro on a non-generic main supplied by that runtime; plain async on main is its own error, not E0131.

A main returning a type that is not a termination type - E0277. fn main() -> i32 { 0 } draws E0277: "main has invalid return type i32", with the note that "main can only return types that implement Termination" and help suggesting () or a Result. Return (), a Result, or ExitCode instead, as shown above.

No main at all - E0601. A file whose only function is named something else, such as fn not_main(), draws E0601, "main function not found in crate", with help suggesting adding a main function. That is an absent entry point, not a malformed one.

One more shape is not an error code at all: #[main] written as an attribute on a plain main fails with the plain error "cannot find attribute main in this scope", noting that main "is in scope, but it is a function, not an attribute". There is no built-in #[main] attribute in this form; runtimes that offer an entry-point attribute provide their own.

The repairs

Remove the parameters from main. This is the whole fix when the generic was not doing real work:

Rust
fn main() {
    println!("plain main");
}
Output
plain main

Move the generic work into a helper. This is the fix when the generic was doing real work - the helper keeps the parameter, the bound, and the body, and main supplies the concrete types:

Rust
fn run<T: std::fmt::Display>(value: T) {
    println!("got {}", value);
}
fn main() {
    run(42);
    run("hello");
}
Output
got 42
got hello

Pick the concrete types in main. If the helper's type was meant to depend on input, read the input first - arguments through std::env::args, a file, or the environment - then branch to a call with the concrete type for that case. main itself stays non-generic in every branch; the choice happens in ordinary code inside its body, where choices can happen.

Fixes in reach order

First, delete the angle brackets from main. If the program then compiles, the generic was decoration and the repair is done.

Second, if the body used the parameter, move that code into a helper that keeps the parameter, and call the helper from main with concrete types. One helper serves as many concrete calls as the program needs.

Third, if the signature also declares value arguments, expect E0580 before or instead of E0131: remove the parameters from the signature and read program arguments with std::env::args inside the body.

Fourth, check the return position separately. Result and ExitCode returns are legal on a plain main; any other return type is E0277, and async is E0752 - neither is fixed by touching generic parameters.

Last, check the code before assuming E0131. A wrong-type main is E0580, an async main is E0752, a bad return type is E0277, and a missing main is E0601.

Decision summary

E0131 fired for every generic list probed on main: the index's fn main<T>(), a bounded T: Clone, two parameters T, U, a where T: Clone clause, a const generic const N: usize, and a lifetime 'a - always with the one header and the span covering exactly the list written. It did not fire for a plain main, a generic helper called from main, a concrete helper, a main returning Result or ExitCode, or a main reading std::env::args, all of which compiled and ran. Neighboring shapes drew their own codes: a generic main with a value argument and a plain main with a value argument drew E0580, an async main drew E0752, a main returning i32 drew E0277, a file with no main drew E0601, and a #[main] attribute drew a plain cannot-find-attribute error with no code.

Checklist

  • main takes no generic parameters of any kind: no type parameters, no const generics, no lifetimes, with or without bounds or a where clause.
  • main takes no value parameters either; read program arguments with std::env::args inside the body.
  • Move generic work into a helper function and call it from main with concrete types - the helper may be as generic as the work needs.
  • A generic main that also takes an argument reports E0580 first; fix the argument, then remove the generic list.
  • main may return (), a Result, or ExitCode. Other return types are E0277; async main is E0752; no main at all is E0601.

Sources

  • Rust error codes index, E0131 - the main function was defined with generic parameters; the erroneous example is fn main<T>(); it is not possible to define the main function with generic parameters, and it must not take any arguments.
  • The Rust Reference, Crates and source files - the entry point of a binary crate and the fixed role of the main function the runtime calls.

Keep Exploring