For example, when i need to create a [String;N]
-like array, just as
let a=[String::from("hello world");10];
the compiler would report that
the trait
Copy
is not implemented forString
but just with trait Clone
, we could do in a proper way:
const DEFUALTSTRING:String = String::new();
pub fn buildarray<const N:usize>(val:String)->[String;N]{
let mut a=[DEFUALTSTRING;N];
for x in &mut a{
x.clone_from(&val);
}
a
}
fn main(){
let a:[String;10]=buildarray(String::from("hello world"));
}
so that we build the array a
and take away the ownership of val
at the same time
but it is not supported in default
and it costs one more time initialization for let mut a=[DEFUALTSTRING;N];
Another problem is that Default::default()
is not a const fn
I don't not why it would be designed like this