3 ms·
How do you copy data from one array of structs to another in rust? Also, if foo is a non-shared pointer that copy doesn't seem safe.
by lonelappde 7y ago
How do you copy data from one array of structs to another in rust?
Also, if foo is a non-shared pointer that copy doesn't seem safe.
- steveklabnik 7y ago> How do you copy data from one array of structs to another in rust? Depends on exactly what you mean; if you're trying to say, replace one part of an array with another, you would use copy_from_slice https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e29e9522d81d3cbe3960e842de758d4b https://play.rust-lang.org/?version=stable&mode=debug&editio... (or clone_from_slice if your struct isn't Copy) Also, your parent is just plain incorrect, Rust does let you do that https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b9d788265b3881a282e46d201dd0b2e5 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- IshKebab 7y agoSorry, you are correct. I was thinking of situations like this: fn assign(a: &mut Bar, b: &Bar) { a.foo = b.foo; } assign(&mut a[0], &a[1]); Bit contrived - next time Rust stops me doing something simple I will note it down! Again to be clear, I'm not saying that there's anything wrong with Rust here.
- steveklabnik 7y agoAh yeah, you can split_at_mut here, but you're right that that's an actual error. My bad!