Skip to content

improve match arm with semi-colon help suggestion #108472

Closed

Description

Code

use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main() {
    println!("Guess the number!");
    
    let secret_number = rand::thread_rng().gen_range(1..=100);
    println!("The secret number is: {secret_number}: ");

    println!("Please input your guess: ");

    let mut guess = String::new();

    io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");
    
    let guess: u32 = guess.trim().parse().expect("Please type a number: ");
   
    println!("You guessed: {guess}");
    match guess.cmp(&secret_number) {
        Ordering::Less => println!("Too small!");
        Ordering::Greater => println!("Too big!"),
        Ordering::Equal => println!("You won!"),
    }
}

Current output

23 |         Ordering::Less => println!("Too small!");
   |                        -- ^^^^^^^^^^^^^^^^^^^^^^- help: use a comma to end a `match` arm expression: `,`
   |                        |  |
   |                        |  this statement is not surrounded by a body
   |                        while parsing the `match` arm starting here

Desired output

error: `match` arm body without braces
  --> src/main.rs:23:27
   |
23 |         Ordering::Less => println!("Too small!");
   |                        -- ^^^^^^^^^^^^^^^^^^^^^^
   |                        |  |
   |                        |  this statement is not surrounded by a body
   |                        while parsing the `match` arm starting here
help: replace `;` with `,`
   |
23 |     Ordering::Less => println!("Too small!"),
   |

Rationale and extra context

The reason why it should be changed is because the new error message makes it clearer you should replace ; with ,. I was following the book, and I encountered this error. I didn't understand how to solve it and asked on the community discord, then a user in the community server then helped me and I managed to fix the error.

Other cases

No response

Anything else?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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