Structs with undefined/universal lifetimes (not higher-ranked)

⚓ Rust    📅 2025-08-31    👤 surdeus    👁️ 3      

surdeus

Hey folks,

consider this code:

use std::marker::PhantomData;

struct HelperContainer<'helper, T> {
    field: T,
    _marker: PhantomData<&'helper ()>,
}

fn buildHelper<'helper, T>(v: T) -> HelperContainer<'helper, impl Sized + use<'helper, T>> {
    HelperContainer {
        field: v,
        _marker: PhantomData,
    }
}

fn main() {
    let x = buildHelper("abc");
}

This compiles but I think it shouldn't or am I wrong? The reason why I think that is because the lifetime 'helper in buildHelper doesn't depend on the input - it can be everything. Nevertheless it gets captures in the RPIT which stands for an actual field in the container.

Regards
keks

1 post - 1 participant

Read full topic

🏷️ Rust_feed