]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/nll/rc-loop.rs
2b746fac4d42610121168999c01e38a199b5f3e1
[rustc.git] / src / test / run-pass / nll / rc-loop.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // A test for something that NLL enables. It sometimes happens that
12 // the `while let` pattern makes some borrows from a variable (in this
13 // case, `x`) that you need in order to compute the next value for
14 // `x`. The lexical checker makes this very painful. The NLL checker
15 // does not.
16
17 #![feature(match_default_bindings)]
18 #![feature(nll)]
19
20 use std::rc::Rc;
21
22 #[derive(Debug, PartialEq, Eq)]
23 enum Foo {
24 Base(usize),
25 Next(Rc<Foo>),
26 }
27
28 fn find_base(mut x: Rc<Foo>) -> Rc<Foo> {
29 while let Foo::Next(n) = &*x {
30 x = n.clone();
31 }
32 x
33 }
34
35 fn main() {
36 let chain = Rc::new(Foo::Next(Rc::new(Foo::Base(44))));
37 let base = find_base(chain);
38 assert_eq!(&*base, &Foo::Base(44));
39 }
40