I have a container:
pub struct Foo<T> {
pub data: Box<[T]>,
}
I would like a method to initialize a new one from an existing slice:
impl<T> Foo<T> {
fn from_slice(slice: &[T]) -> Foo<T> {
Foo {
data: Box::new(/* something here */),
}
}
}
I'd like to create a Foo
instance from any kind of slice, coming from a dynamic vector or a static string.
I suppose there is a reason why vec!
is a macro, but is there a way to avoid writing one? I guess I could do slice.to_vec().into_boxed_slice()
, but it doesn't seem right to create a Vec
as a proxy to a clone...
I'm not using a Vec
in my struct because the data
isn't supposed to change in size during the lifetime of my container. It didn't feel right to use a Vec
but I may be wrong.