Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 19, 2019 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/34ea2c5f838e293fcb0b63329193f807 to your computer and use it in GitHub Desktop.
Save rust-play/34ea2c5f838e293fcb0b63329193f807 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
pub struct Thing {
// many big fields here
}
// I can't change this definition
pub struct Wrapper {
pub thing: Thing, // I can't change this to a reference, it's used elsewhere as a container
// other fields
}
impl Wrapper {
pub fn act(&self){
}
}
fn main() {
let v = vec![Thing{}, Thing{}, Thing{}, Thing{}]; // more elements
let z = 2;
for i in 0..z {
// I temporarly need a wrapper from v[i] to call act
// but I don't want to consume my vector so I want to
// put it back
// I don't want to clone the thing
let w = Wrapper {
thing: v[i] // can't work because it would be a move
};
w.act();
// I don't need w anymore
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment