Skip to content

Commit cba8256

Browse files
committed
add some run-pass tests for NLL showing that things work as expected
1 parent 80c510e commit cba8256

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/test/run-pass/nll/get_default.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
#![feature(nll)]
12+
13+
use std::collections::HashMap;
14+
15+
fn get_default(map: &mut HashMap<usize, String>, key: usize) -> &mut String {
16+
match map.get_mut(&key) {
17+
Some(value) => value,
18+
None => {
19+
map.insert(key, "".to_string());
20+
map.get_mut(&key).unwrap()
21+
}
22+
}
23+
}
24+
25+
fn main() {
26+
let map = &mut HashMap::new();
27+
map.insert(22, format!("Hello, world"));
28+
map.insert(44, format!("Goodbye, world"));
29+
assert_eq!(&*get_default(map, 22), "Hello, world");
30+
assert_eq!(&*get_default(map, 66), "");
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
#![feature(nll)]
12+
13+
use std::collections::HashMap;
14+
15+
fn process_or_insert_default(map: &mut HashMap<usize, String>, key: usize) {
16+
match map.get_mut(&key) {
17+
Some(value) => {
18+
process(value);
19+
}
20+
None => {
21+
map.insert(key, "".to_string());
22+
}
23+
}
24+
}
25+
26+
fn process(x: &str) {
27+
assert_eq!(x, "Hello, world");
28+
}
29+
30+
fn main() {
31+
let map = &mut HashMap::new();
32+
map.insert(22, format!("Hello, world"));
33+
map.insert(44, format!("Goodbye, world"));
34+
process_or_insert_default(map, 22);
35+
process_or_insert_default(map, 66);
36+
assert_eq!(map[&66], "");
37+
}

src/test/run-pass/nll/rc-loop.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+

0 commit comments

Comments
 (0)