Option<&mut T> to *mut T


#1

Is there a standard library function/methods for getting a *mut T out of an Option<&mut T>? I.e. None turns into a null pointer. That is, the inverse of as_ref on primitive pointer types?


#2

Would Option::map_or work for this?

use std::ptr;

fn main() {
    let mut x = 7i32;
    let xr = Some(&mut x);
    let xp = xr.map_or(ptr::null(), |r| r as *mut i32);
    
    println!("{:?}", xp);
}

#3

Yes, but I’m trying to find out if there’s a standard library method that does that and hides the boilerplate.


#4

map_or and other Option method that take Fn* is standard library method
to convert Option to something else.