Created
May 19, 2019 16:26
-
-
Save rust-play/34ea2c5f838e293fcb0b63329193f807 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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