Skip to content

Instantly share code, notes, and snippets.

@yoshuawuyts
Created March 25, 2018 08:25
Show Gist options
  • Save yoshuawuyts/88fdcff4d0099e8060f10cc4ef60e314 to your computer and use it in GitHub Desktop.
Save yoshuawuyts/88fdcff4d0099e8060f10cc4ef60e314 to your computer and use it in GitHub Desktop.
// This version gives a "cannot move out of borrowed content error"
let ref fd = &self.fd.unwrap();
// But I guess that's fair game. Docs say it "moves" content, which
// is not what we want. How about expect?
let ref fd = &self.fd.expect("Invalid state: no fd found in Sync::Write");
// Nope, also doesn't work. This is the only version that works fine.
let fd = match &self.fd {
&Some(ref fd) => fd,
&None => panic!("Invalid state: no fd found in Sync::Write"),
};
@yoshuawuyts
Copy link
Author

Solution was to do:

    let fd = self.fd.as_ref().expect("self.fd was None.");

Which gives us a reference that can be moved by .expect(). Autoref prevents the need to then call it let ref fd =

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