About the language: why doesn't never type automatically implement any trait?

⚓ Rust    📅 2026-06-28    👤 surdeus    👁️ 2      

surdeus

Hello there!

As we know, the never type (!) can be used as any type in Rust. However it appears that the never type can not satisfy every trait bound, which is counter-intuitive. Is there a reason why the feature is missing in the language?

// does not compile
fn test1() {
    trait MyTrait {
        fn my_method();
    }

    fn requires_my_trait<T: MyTrait>(_: T) {}

    let never = unreachable!();

    requires_my_trait(never);
}

// compiles
fn test2() {
    trait MyTrait {
        fn my_method();
    }

    fn requires_my_trait<T: MyTrait>(_: T) {}

    impl MyTrait for String {
        fn my_method() {
            todo!()
        }
    }

    let never = unreachable!();

    let as_string: String = never;

    requires_my_trait(as_string);
}

// does not compile
fn test3() {
    trait MyTrait {
        fn my_method();
    }

    fn requires_my_trait<T: MyTrait>(_: T) {}

    impl MyTrait for ! {
        fn my_method() {
            todo!()
        }
    }

    let never = unreachable!();

    requires_my_trait(never);
}

2 posts - 2 participants

Read full topic

🏷️ Rust_feed