Skip to content

Instantly share code, notes, and snippets.

@jpastuszek
Forked from rust-play/playground.rs
Created July 8, 2019 09:41
Show Gist options
  • Save jpastuszek/559bc637c2715248bac62822a710ad36 to your computer and use it in GitHub Desktop.
Save jpastuszek/559bc637c2715248bac62822a710ad36 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
// https://stackoverflow.com/questions/50519147/double-mutable-borrow-error-in-a-loop-happens-even-with-nll-on
// https://github.com/rust-lang/rust/issues/54663
// This will complie with polonius: rustc +nightly -Zpolonius --edition=2018 /tmp/p.rs
struct Foo(u8);
impl Foo {
fn even<'i>(&'i mut self) -> &'i u8 {
loop {
match self.next() {
Some(even) => return even,
None => continue,
}
}
}
fn next<'i>(&'i mut self) -> Option<&'i u8> {
self.0 += 1;
if self.0 % 2 == 0 {
Some(&self.0)
} else {
None
}
}
}
fn main() {
let mut foo = Foo(1);
println!("{:?}", foo.next());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment