Howdy,
I've been looking into Rust for a while, but in practicing I've noticed that while I have a pretty solid understanding of how to deal with simple structures containing primitives, dealing with larger and slightly more complex structures is still a bit of an enigma to me.
For example, I had a structure set up like below to represent a tile on a chess board:
struct Tile {
piece: Option<ChessPiece>,
color: ColorEnum,
}
and I was trying to make a method for the function that would swap the piece
elements between two Tile
structs, like below:
impl Tile {
fn swap_pieces(&mut self, other: &mut Tile) {
// hmm...
}
}
I realized at this point that what I would normally do is just swap the pointers, but that isn't as simple in Rust. I found solutions online like doing:
mem::swap(&mut self.piece, &mut other.piece)
...which seemed to work fine, but it feels like there should be some alternative.
Is there some other way to transfer ownership of members between two structures? Or rather, is there a better way to think about data structures in Rust, particularly in modifying fields?
Thanks for any insights!