6556f26991
Update Box::from_raw example to generalize better I know very little about rust, so I saw the example here ``` use std::alloc::{alloc, Layout}; unsafe { let ptr = alloc(Layout:🆕:<i32>()) as *mut i32; *ptr = 5; let x = Box::from_raw(ptr); } ``` and tried to generalize it by writing, ``` let layout = Layout:🆕:<T>(); let new_obj = unsafe { let ptr = alloc(layout) as *mut T; *ptr = obj; Box::from_raw(ptr) }; ``` for some more complicated `T`, which ended up crashing with SIGSEGV, because it tried to `drop_in_place` the previous object in `ptr` which is of course garbage. I think that changing this example to use `.write` instead would be a good idea to suggest the correct generalization. It is also more consistent with other documentation items in this file, which use `.write`. I also added a comment to explain it, but I'm not too attached to that, and can see it being too verbose in this place. |
||
---|---|---|
.. | ||
alloc | ||
benches | ||
collections | ||
prelude | ||
raw_vec | ||
rc | ||
sync | ||
tests | ||
alloc.rs | ||
borrow.rs | ||
boxed.rs | ||
Cargo.toml | ||
fmt.rs | ||
lib.rs | ||
macros.rs | ||
raw_vec.rs | ||
rc.rs | ||
slice.rs | ||
str.rs | ||
string.rs | ||
sync.rs | ||
task.rs | ||
tests.rs | ||
vec.rs |