Commit 0c88dd6
authored
Update Box::from_raw example to generalize better
I know very little about rust, so I saw this example and tried to generalize it by writing,
```
let layout = Layout::new::<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 also added a comment that explains why `.write`
is used, but I think adding that comment is optional and may be too verbose
here. I do however think that changing this example is a good idea to
suggest the correct generalization. `.write` is also used in most of the rest
of the documentation here, even if the example is `i32`, so it would additionally
be more consistent.1 parent 0c04344 commit 0c88dd6
1 file changed
+4
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
382 | 382 | | |
383 | 383 | | |
384 | 384 | | |
385 | | - | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
386 | 389 | | |
387 | 390 | | |
388 | 391 | | |
| |||
0 commit comments