• kcuf2@lemmynsfw.com
    link
    fedilink
    arrow-up
    1
    arrow-down
    3
    ·
    1 day ago

    It’s because they wanted to hack control flow functionality into expressions. Returning None is actually returning something, but never is just a placeholder for any type when they want to do things that may exit the expression entirely. This is an example in the docs

    let num: u32 = match get_a_number() {
        Some(num) => num,
        None => break,
    };
    

    Break exits the expression without ever producing a value.

    This is an unfortunate wart to appease a desire to those that want to be able to write code like they do in legacy languages. There should have been better ways to do this without being a hack IMO

    • SorteKanin@feddit.dk
      link
      fedilink
      arrow-up
      4
      ·
      edit-2
      1 day ago

      I think you’re misunderstanding the never type. The never type is not a hack at all. It’s a very natural part of the type system. Just as you have the unit type (), which is the canonical type with only 1 value, you also have the never type, the canonical type with 0 values.

      This is extremely useful in generic code. See my other comment in this thread.

      This is an unfortunate wart to appease a desire to those that want to be able to write code like they do in legacy languages

      What do you mean with this? I can’t really decipher it. What alternative to the never type would you want?