How can I make the code refused

⚓ Rust    📅 2025-12-04    👤 surdeus    👁️ 2      

surdeus

There are UB in the code, but rustc allows it, so there are errors in in my code
I think phantomdata may help, turns out it does not, at least used like here

use std::marker::PhantomData;
use std::fmt::Display;
use std::ptr::NonNull;

struct A<'a, T: Display> {
    a: NonNull<T>,
    b: PhantomData<&'a mut T> 
}

impl<'a, T: Display> Drop for A<'a, T> {
    fn drop(&mut self) {
        println!("{}", unsafe {self.a.as_ref()});
    }
}

fn main() {
    let x: A<String>;
    let mut s = "string".into();

    x = A {
        a: NonNull::new(&mut s).unwrap(),
        b: PhantomData
    };
}

4 posts - 3 participants

Read full topic

🏷️ Rust_feed