lotsoftools

Understanding Rust Error E0137: Multiple Functions with #[main]

What is Rust Error E0137?

Rust Error E0137 occurs when the compiler detects more than one function with the #[main] attribute. In Rust, there must be exactly one unique entry point, which is the function marked with the #[main] attribute. Having multiple entry points in a program leads to this error.

An Example of Erroneous Code

#![allow(unused)]
#![feature(main)]

fn main() {
  #[main]
  fn foo() {}

  #[main]
  fn f() {} 
}

In the above example, the program contains two functions, foo() and f(), both marked with the #[main] attribute. This causes Rust Error E0137.

How to Fix Rust Error E0137

To fix Rust Error E0137, ensure that only one function has the #[main] attribute. In most cases, this will be the main() function in your program.

An Example of Valid Code

#![allow(unused)]
#![feature(main)]

fn main() {
  #[main]
  fn f() {} 
}

In the above example, there is only one function with the #[main] attribute. This program will now compile and run without Rust Error E0137.