Created
February 10, 2018 23:45
-
-
Save aishraj/b6202dffae40185ba29ac82d129ab68d to your computer and use it in GitHub Desktop.
Swap 2 fields of a struct rust
This file contains hidden or 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
#[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