Syntax and code question

I'm looking at line 14 of this code, playground link

  1. where is "size" coming from ? there is no formal declaration anywhere.
  2. I don't understand the syntax "size if size..."
enum Star {
    BrownDwarf = 10,
    RedDwarf = 50,
    YellowStar = 100,
    RedGiant = 1000,
    DeadStar, // Think about this one. What number will it have?

fn main() {
    use Star::*;
    let starvec = vec![BrownDwarf, RedDwarf, YellowStar, RedGiant];
    for star in starvec {
        match star as u32 {
            size if size <= 80 => println!("Not the biggest star."), // Remember: size doesn't mean anything. It's just a name we chose so we can print it
            size if size >= 80 => println!("This is a good-sized star."),
            _ => println!("That star is pretty big!"),
    println!("What about DeadStar? It's the number {}.", DeadStar as u32);


In "size if size < ..." the first size is a pattern binding the matched value to the identifier "size".

The trailing "if size < ..." Is called a match guard iirc.

The name is arbitrary and you can choose whatever.
e.g. "my_size if my_size < ..."


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.