-
-
Save szabba/ac2030897e5bd929880d to your computer and use it in GitHub Desktop.
Shared via 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
// Can I have two mut references to different parts of a structure | |
// simultaneously? | |
struct Point { | |
x: f64, | |
y: f64, | |
} | |
fn main() { | |
let mut p = Point{x: 3.0, y: 5.0}; | |
println!("({}, {})", p.x, p.y); | |
{ | |
swap(&mut p.x, &mut p.y); | |
} | |
println!("({}, {})", p.x, p.y); | |
} | |
fn swap(a: &mut f64, b: &mut f64) { | |
let t = *b; | |
*b = *a; | |
*a = t; | |
} | |
// YES I CAN! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment