logoalt Hacker News

ustad • today at 7:23 PM • 6 replies • view on HN

“Never type” - Feel like I’m stupid for not having a clue what that means.


Replies

Xirdus • today at 8:04 PM

Mathematically, types are sets of possible values.

Any type (like in TypeScript; Rust doesn't have one) is a set of every possible value; a supertype of all types. Every expression regardless of type can be assigned to a variable of type any.

The opposite is the never type, which is an empty set; no value is of type never. Per set theory, an empty set is a subset of every set, which makes the never type a subtype of all types. An expression of type never can be assigned to every variable regardless of type.

If never type has no values, then how can an expression have never type? Easy: it must never evaluate. Infinite loops, unconditional panics/exceptions, process exit/abort, etc. Also, generics - you can have Vec<!> that's always empty, Result<T,!> that can never be an error, etc.

➕ show 3 replies
anamexis • today at 7:26 PM

The explanation in OP seems pretty solid.

https://blog.rust-lang.org/2026/11/12/Rust-1.100.0/#never-ty...

pkulak • today at 7:26 PM

A few other langs have it too. It's just something to return when no return is actually possible. And then it can propagate, so it makes some nice things possible that weren't before.

➕ show 1 reply
yccs27 • today at 9:07 PM

If you have a variable x: ! in scope, your code can never be reached.

LoganDark • today at 7:49 PM

It's the type called "!", or the return value of diverging control flow, such as infinite loops, returns or function calls that panic or abort. It's an uninhabited type, meaning no values of it can actually exist, and that any code that would encounter such a value is unreachable.

NooneAtAll5 • today at 8:26 PM

noreturn, basically