Skip to content

Confusing suggestion when if is missing in else if #49361

Closed
@stokhos

Description

@stokhos
pub fn nth(n:u32)->Option<u32>{
    let mut x:u32 = 2;
    let mut m:u32 = 1;
    let mut p:Vec<u32> = vec![2];
    'outer: loop {
        x += 1;
        'inner: for i in p.iter() {
            if x % i ==0{
                continue 'outer;
            }
        }
        p.push(x);
        m+=1;
        if n < 1 {
            return None;        
        } else if n == 1 {
            return Some(2);
        } else m == n { // should be else if here
            return Some(x);
        }
    }
}

fn main(){
    println!("{:?}", nth(1));
}

The error here is if is missing in else if

but compiler reports: error:

error: expected identifier, found keyword `return`
  --> src/main.rs:19:13
   |
19 |             return Some(x);
   |             ^^^^^^

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions