Fixed: "there is a keyword `else` with a similar name" for elseif in Rust
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:
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:

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:
cargo run -q
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

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:
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");
}
}
cargo run -q
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

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 afterelse: a block, or anotherifexpression. There's noelseif. - 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. Butelsecan't be followed by a condition, so the suggested code was still wrong. The spellings of other languages,elsifandelif, 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 suggestelse if.
The Fix
Write else if as two words:
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
cargo run -q
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,elsiforelif: the only spelling iselse 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 ifgrows long, consider amatch. 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
| Symptom | Likely cause | Fix |
|---|---|---|
expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `amount` and help: there is a keyword `else` with a similar name | elseif written as one word | Write else if |
expected `{`, found `amount` and expected an `if` or a block after this `else` | else followed straight by a condition | Add if after else |
The same first error for elsif or elif, with no help | A spelling from another language | Write 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
- if expressions, in The Rust Reference.
- Handling Multiple Conditions with else if, in The Rust Programming Language.
- Rust issue #166990, about the suggestion of
elseforelseif.
