Skip to content

Instantly share code, notes, and snippets.

@cathay4t
Created March 24, 2021 06:54
Show Gist options
  • Save cathay4t/23e38d04f47371643bda3193ac6d6c08 to your computer and use it in GitHub Desktop.
Save cathay4t/23e38d04f47371643bda3193ac6d6c08 to your computer and use it in GitHub Desktop.
interview_rust.rs
struct VlanInfo {
base_iface: String,
vlan_id: u16,
}
struct Iface {
name: String,
vlan_info: Option<VlanInfo>,
}
fn change_base_iface(iface: &mut Iface) {
if let Some(mut vlan_info) = &iface.vlan_info {
vlan_info.base_iface = "eth2".into();
}
}
fn main() {
let mut iface = Iface {
name: "foo1".into(),
vlan_info: Some(VlanInfo {
base_iface: "eth1".into(),
vlan_id: 1u16,
}),
};
change_base_iface(&mut iface);
if let Some(vlan_info) = iface.vlan_info {
println!("{}", vlan_info.base_iface);
}
}
@cathay4t
Copy link
Author

cathay4t commented Mar 24, 2021

   Compiling hello v0.1.0 (/home/fge/Source/rust_play)
error[E0507]: cannot move out of a shared reference
  --> main.rs:12:34
   |
12 |     if let Some(mut vlan_info) = &iface.vlan_info {
   |                 -------------    ^^^^^^^^^^^^^^^^
   |                 |
   |                 data moved here
   |                 move occurs because `vlan_info` has type `VlanInfo`, which does not implement the `Copy` trait

error: aborting due to previous error

For more information about this error, try `rustc --explain E0507`.
error: could not compile `hello`

To learn more, run the command again with --verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment