Hi there,
I am looking for an easy way to initialize an array of structs (I do not want to type out, as it is pretty large). I isolated my problem to this code:
#[derive(Debug)]
struct TestStruct {
a: i32,
b: f32,
c: char,
d: bool
}
impl TestStruct {
fn new() -> TestStruct {
TestStruct {a: 1, b: 1.0, c: 'c', d: true}
}
}
fn main() {
let test_var = TestStruct::new();
let test_array = [TestStruct::new(); 20];
println!("test_var: {:#?}", test_var);
println!("test_array: {:#?}", test_array);
}
The code does not compile, error is, that there is no copy trait for TestStruct. Two questions:
- Why do I need a copy trait? I actually want it to call 20 times the new function!?
- I also failed to find information how to actually implement the copy trait, I only found clone, is it the same?
Thanks & regards
PS: Answer to question 1) might be this:
= note: the Copy
trait is required because the repeated element will be copied
(Seems the way how Rust implements that syntax above, obviously it indeed creates it once, and then copies it.... than I would change question 1 to: Is there another/better way to do this?