• SorteKanin@feddit.dk
    link
    fedilink
    arrow-up
    6
    ·
    1 day ago

    shouldn’t Rust enforce returning from function

    This is impossible. How would you enforce that? What prevents a function from panicking, aborting the whole process or just going into an infinite loop? All these things correspond to the never type.

    I think you’re misunderstanding what the never type is. It’s not equivalent to None at all. It’s a type that doesn’t have any values. This is useful in situations with generic code where for example an error type can be chosen as the never type. Then you could destructure or unwrap a Result without handling the error, cause the type system guarantees the error never occurs.

    I think you should read up on the never type a bit more. It’s a perfectly natural part of the type system. In fact you can make your own very easily: enum Never {}