Created
June 1, 2021 00:21
-
-
Save Verdagon/ff673587ae340ff3c044b9f9073396c1 to your computer and use it in GitHub Desktop.
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
struct Spaceship { | |
subsystem: Subsystem | |
} | |
enum Subsystem { | |
Engine { fuel: i64 }, | |
Navigation { antenna: Box<Antenna> }, | |
} | |
struct Antenna { | |
frequency: i64 | |
} | |
fn main() -> () { | |
let mut ship = | |
Spaceship{ | |
subsystem: Subsystem::Navigation{ | |
antenna: Box::new( | |
Antenna { frequency: 100 } | |
) | |
} | |
}; | |
match &ship.subsystem { | |
Subsystem::Navigation{antenna} => { | |
ship.subsystem = Subsystem::Engine{fuel: 10}; | |
antenna.frequency = 200; | |
println!("{:?}", antenna.frequency); | |
} | |
Subsystem::Engine{fuel} => { | |
println!("{:?}", fuel); | |
} | |
} | |
} | |
// error[E0506]: cannot assign to `ship.subsystem` because it is borrowed | |
// --> enumunsafety.rs:22:7 | |
// | | |
// 20 | match &ship.subsystem { | |
// | --------------- borrow of `ship.subsystem` occurs here | |
// 21 | Subsystem::Navigation{antenna} => { | |
// 22 | ship.subsystem = Subsystem::Engine{fuel: 10}; | |
// | ^^^^^^^^^^^^^^ assignment to borrowed `ship.subsystem` occurs here | |
// 23 | | |
// 24 | antenna.frequency = 200; | |
// | ----------------------- borrow later used here | |
// | |
// error[E0594]: cannot assign to `antenna.frequency` which is behind a `&` reference | |
// --> enumunsafety.rs:24:7 | |
// | | |
// 24 | antenna.frequency = 200; | |
// | ^^^^^^^^^^^^^^^^^^^^^^^ `antenna` is a `&` reference, so the data it refers to cannot be written | |
// | |
// error: aborting due to 2 previous errors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment