educative.io

What's the difference between

impl Days{
// if the day is a weekend
fn is_weekend(&self)->i32{
  match self{
    &Days::Saturday=>return 1,
    &Days::Sunday=>return 1,
    _=>return 0
  }
}

}
In this example what is the difference between using the borrow operator and not for enum types?

Hi @Julian_Mourell,
Welcome to the Discuss community.

The borrow operator (&) is normally used to create a reference to the value instead of taking complete ownership of the enum. It is commonly used in complex data structure problems like pattern matching, where we avoid the call to the value.

In this code example, we used the same approach by using the borrow operator in comparison, which doesn’t make any change in the functionality. It saves the reference to the enum variants. The pattern matching will work absolutely fine without the borrow operator as well.

I hope you understand why we use it in the code. If you’ve any other queries, please feel free to ask.