Stabilizing Rust's Never Type

(lwn.net)

82 points | by cjd8 3 days ago

5 comments

  • LatticeAnimal 4 minutes ago
    Is it obvious to rust developers that "!" would be the never type? I frequently use "never" in typescript. I could imagine using the never type frequently in rust too. I feel like a longer more human-understandable name would've been a good decision here. (feels like more rust jargon that makes the language harder to learn)
  • xg15 50 minutes ago
    > After this change (and on the 2024 edition), the compiler assumes that T should be !, which doesn't implement Default, and therefore causes a compilation error.

    If ! can coerce to every type, why not treat it as if it implemented every trait too?

    • tux3 48 minutes ago
      The Default trait provides a function that actually constructs the type in question. But here the ! type can never be constructed, so the only way to implement Default would be to have it panic, loop infinitely, or otherwise fail at runtime.

      So this would risk turning a compile-time error into a runtime error.

      • dlubarov 45 minutes ago
        Moreover, Rust traits' associated constants/types get in the way of having a proper bottom type. What would <! as Iterator>::Item be? (In Scala I think it just doesn't compile?)
      • xg15 44 minutes ago
        Ah, that makes sense. Rust noob here, so I wasn't aware traits can act on types directly without any instance of the type. Thanks for the info!
        • Sharlin 11 minutes ago
          Yep, they can have static methods, as it were (in Rust lingo called "associated functions"; "methods" in Rust always take a `self` receiver). Traits can also have associated types and associated constants, which (naturally) also relate to the type, not any particular instance.
  • weinzierl 34 minutes ago
    Relevant talk by Waffle at RustWeek earlier this year:

    "When is never?"

    https://youtube.com/watch?v=3jM4cnEVrLc

  • munchler 13 minutes ago
    As a fan of the Curry-Howard correspondence, I approve of this decision.
  • epolanski 53 minutes ago
    The never type seems very useful in various languages to either signal that a branch can never happen (the example of string -> bytestring never erroring) or to mark that a function will never return a value (and thus control) to the caller.

    A simple TypeScript example:

    const forever = (): never => { while (true) { // whatever } }