Looking at the methods available for Vec<T>
I stumbled across
into_boxed_slice(self) -> Box<[T]>
String
also has such a method (into_boxed_str(self)
). The usefulness of having Deref
for Vec<T>
/String
that allows them to be treated like a shared slice (&[T]
) is obvious, but I don't see any use for an owned slice (Box<[T]>
) except, perhaps, FFI. The Rust GitHub repo only uses into_boxed_slice()
in a handful of cases.
Since methods for creating boxed slices are available in std
and this container is listed on its main page, I thought that I might be missing something useful about it. What are cases where I should use an owned slice in favor of a Vec<T>
or a String
?
Box<[T]>
andBox<str>
have one less pointer-sized integer as they don't need a capacity — they can't be resized. – ArchaeornisBox
and[T]
orstr
. It's not really that different from aBox<Trait>
. – Archaeornisstd
, which gives it a feeling of some nobility :). That's what got me interested in it. – SkewBox::from_raw(&mut vec[..] as *mut [T])
, and then manually forget the Vec, which is just unpleasant. It's nicer to just provide a safe alternative. – Choi