Skip to main content

Fixed: "there is a keyword `else` with a similar name" for elseif in Rust

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Rust has no elseif keyword. Writing } elseif amount == 500 { stopped the build with help: there is a keyword `else` with a similar name, and the code that help suggests, } else amount == 500 {, fails with a second error. Rust spells it as two words, else if.

What I Was Doing​

I was trying out the mistakes people make in an if chain, for a guide on Rust's if. One of them is writing else if as one word, elseif. I followed the compiler's help to see where it leads.

What I Expected​

An error on the misspelled word, with a help that suggests else if.

What Happened Instead​

Create a project:

Terminal
cargo new demo
cd demo

cargo new demo creates a project in a folder named demo, with its code in src/main.rs, and cd demo moves into it.

Replace the contents of src/main.rs with:

src/main.rs, which sets amount to 500 and checks it with if amount is greater than 500, then elseif written as one word for amount equal to 500, then else, printing over, exactly at or under the limit.

src/main.rs
fn main() {
let amount = 500;
if amount > 500 {
println!("over the limit");
} elseif amount == 500 {
println!("exactly at the limit");
} else {
println!("under the limit");
}
}

Run it:

Terminal
cargo run -q
Output
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `amount`
--> src/main.rs:5:14
|
5 | } elseif amount == 500 {
| ^^^^^^ expected one of 8 possible tokens
|
help: there is a keyword `else` with a similar name
|
5 - } elseif amount == 500 {
5 + } else amount == 500 {
|

error: could not compile `demo` (bin "demo") due to 1 previous error

A zsh terminal where cargo run -q fails with expected one of 8 possible tokens, found amount, on the elseif line, and a help saying there is a keyword else with a similar name, which suggests replacing elseif with else.

To the compiler, elseif isn't a keyword but an ordinary name. After a name it expected something like an operator or a ;, and found amount. The help suggested replacing elseif with else, so I did that:

src/main.rs
fn main() {
let amount = 500;
if amount > 500 {
println!("over the limit");
} else amount == 500 {
println!("exactly at the limit");
} else {
println!("under the limit");
}
}
Terminal
cargo run -q
Output
error: expected `{`, found `amount`
--> src/main.rs:5:12
|
5 | } else amount == 500 {
| ---- ^^^^^^
| |
| expected an `if` or a block after this `else`
|
help: add an `if` if this is the condition of a chained `else if` statement
|
5 | } else if amount == 500 {
| ++

error: could not compile `demo` (bin "demo") due to 1 previous error

A zsh terminal where cargo run -q, with else in place of elseif, fails with expected an if or a block after this else, and a help that suggests adding if to make it else if.

The suggested code didn't compile either. After else, Rust expects an if or a block, and this time the help suggested the right code: else if.

What I Checked​

  • The syntax of if. The Rust Reference, if expressions: "The syntax of an if expression is a sequence of one or more condition operands separated by &&, followed by a consequent block, any number of else if conditions and blocks, and an optional trailing else block." Its grammar allows two things after else: a block, or another if expression. There's no elseif.
  • The Rust Book, Handling Multiple Conditions with else if: "You can use multiple conditions by combining if and else in an else if expression."
  • The first help. It named the keyword closest to the word the compiler didn't know, else. But else can't be followed by a condition, so the suggested code was still wrong. The spellings of other languages, elsif and elif, gave the same first error in my checks, with no help at all.
  • Whether it's reported. I found no report of it, so I reported it as Rust issue #166990: for elseif, the help should suggest else if.

The Fix​

Write else if as two words:

src/main.rs
fn main() {
let amount = 500;
if amount > 500 {
println!("over the limit");
} else if amount == 500 {
println!("exactly at the limit");
} else {
println!("under the limit");
}
}

Only line 5 changed: } else if amount == 500 {.

The Result​

Terminal
cargo run -q
Output
exactly at the limit

A zsh terminal where cargo run -q prints exactly at the limit.

The program compiled and printed exactly at the limit, because amount is 500.

How to Avoid It​

  • Rust has no elseif, elsif or elif: the only spelling is else if.
  • Treat a help that names a keyword as a hint, not as finished code, and build again after you apply it.
  • When a chain of else if grows long, consider a match. The Book says: "Using too many else if expressions can clutter your code, so if you have more than one, you might want to refactor your code."

Troubleshooting​

SymptomLikely causeFix
expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `amount` and help: there is a keyword `else` with a similar nameelseif written as one wordWrite else if
expected `{`, found `amount` and expected an `if` or a block after this `else`else followed straight by a conditionAdd if after else
The same first error for elsif or elif, with no helpA spelling from another languageWrite else if

FAQ​

Is elseif a keyword in Rust?​

No. The keywords listed in The Rust Reference include else and if, but not elseif, elsif or elif.

Can I put an if inside the else block instead?​

Yes. An else block holding its own if and else printed the same exactly at the limit. else if does the same with one level of braces less.

Conclusion​

Rust spells it else if, two words. elseif stops the build, and the help's else leads to a second error, whose help finally suggests else if.

Additional Resources​