Skip to content

Commit 74f175e

Browse files
committed
Add text about collecting directly into a boxed slice.
Heavily inspired by #99.
1 parent 12263da commit 74f175e

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/type-sizes.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,16 @@ assert_eq!(size_of_val(&v), 3 * size_of::<usize>());
140140
let bs: Box<[u32]> = v.into_boxed_slice();
141141
assert_eq!(size_of_val(&bs), 2 * size_of::<usize>());
142142
```
143-
The boxed slice can be converted back to a vector with [`slice::into_vec`]
144-
without any cloning or a reallocation.
143+
Alternatively, a boxed slice can be constructed directly from an iterator with
144+
[`Iterator::collect`], avoiding the need for any reallocation.
145+
```rust
146+
let bs: Box<[u32]> = (1..3).collect();
147+
```
148+
A boxed slice can be converted to a vector with [`slice::into_vec`] without any
149+
cloning or reallocation.
145150

146151
[`Vec::into_boxed_slice`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.into_boxed_slice
152+
[`Iterator::collect`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect
147153
[`slice::into_vec`]: https://doc.rust-lang.org/std/primitive.slice.html#method.into_vec
148154

149155
## `ThinVec`

0 commit comments

Comments
 (0)