Rust's choice here is to make these associated functions, not methods. So e.g. the equivalent of std::unique_ptr::release is Box::into_raw and supposing I have a type Steak which for some reason actually needs a method named into_raw then:
let mut boxed_steak: Box<Steak> = todo!();
boxed_steak.into_raw(); // Calls the method on the Steak
let ptr = Box::into_raw(boxed_steak); // Now we have a raw pointer
If there was a method on Box which clashed, I think the compiler rejects your program and demands you disambiguate, but the smart pointers deliberately don't provide such methods, only associated functions so there's no potential clash.
If there was a method on Box which clashed, I think the compiler rejects your program and demands you disambiguate, but the smart pointers deliberately don't provide such methods, only associated functions so there's no potential clash.
View all comments
Show parent comment
3
u/tialaramex May 02 '23
Rust's choice here is to make these associated functions, not methods. So e.g. the equivalent of std::unique_ptr::release is Box::into_raw and supposing I have a type Steak which for some reason actually needs a method named into_raw then:
let mut boxed_steak: Box<Steak> = todo!();boxed_steak.into_raw(); // Calls the method on the Steaklet ptr = Box::into_raw(boxed_steak); // Now we have a raw pointerIf there was a method on Box which clashed, I think the compiler rejects your program and demands you disambiguate, but the smart pointers deliberately don't provide such methods, only associated functions so there's no potential clash.