Skip to content

Instantly share code, notes, and snippets.

@aishraj
Created February 10, 2018 23:45
Show Gist options
  • Save aishraj/b6202dffae40185ba29ac82d129ab68d to your computer and use it in GitHub Desktop.
Save aishraj/b6202dffae40185ba29ac82d129ab68d to your computer and use it in GitHub Desktop.
Swap 2 fields of a struct rust
#[derive(Debug)]
pub struct Demo {
f: i32
}
#[derive(Debug)]
pub struct Other {
a: Demo,
b: Demo
}
impl Other {
fn swapp(&mut self) {
std::mem::swap(&mut self.a, &mut self.b);
}
}
fn main() {
let mut s = Other{a: Demo{f: 0}, b: Demo{f: 1}};
println!("{:?}",s);
s.swapp();
println!("{:?}",s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment