mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-17 17:33:07 +00:00
add some run-pass tests for NLL showing that things work as expected
This commit is contained in:
parent
80c510e353
commit
cba82561cf
31
src/test/run-pass/nll/get_default.rs
Normal file
31
src/test/run-pass/nll/get_default.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
#![feature(nll)]
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
fn get_default(map: &mut HashMap<usize, String>, key: usize) -> &mut String {
|
||||||
|
match map.get_mut(&key) {
|
||||||
|
Some(value) => value,
|
||||||
|
None => {
|
||||||
|
map.insert(key, "".to_string());
|
||||||
|
map.get_mut(&key).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let map = &mut HashMap::new();
|
||||||
|
map.insert(22, format!("Hello, world"));
|
||||||
|
map.insert(44, format!("Goodbye, world"));
|
||||||
|
assert_eq!(&*get_default(map, 22), "Hello, world");
|
||||||
|
assert_eq!(&*get_default(map, 66), "");
|
||||||
|
}
|
37
src/test/run-pass/nll/process_or_insert_default.rs
Normal file
37
src/test/run-pass/nll/process_or_insert_default.rs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
#![feature(nll)]
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
fn process_or_insert_default(map: &mut HashMap<usize, String>, key: usize) {
|
||||||
|
match map.get_mut(&key) {
|
||||||
|
Some(value) => {
|
||||||
|
process(value);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
map.insert(key, "".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process(x: &str) {
|
||||||
|
assert_eq!(x, "Hello, world");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let map = &mut HashMap::new();
|
||||||
|
map.insert(22, format!("Hello, world"));
|
||||||
|
map.insert(44, format!("Goodbye, world"));
|
||||||
|
process_or_insert_default(map, 22);
|
||||||
|
process_or_insert_default(map, 66);
|
||||||
|
assert_eq!(map[&66], "");
|
||||||
|
}
|
40
src/test/run-pass/nll/rc-loop.rs
Normal file
40
src/test/run-pass/nll/rc-loop.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
// A test for something that NLL enables. It sometimes happens that
|
||||||
|
// the `while let` pattern makes some borrows from a variable (in this
|
||||||
|
// case, `x`) that you need in order to compute the next value for
|
||||||
|
// `x`. The lexical checker makes this very painful. The NLL checker
|
||||||
|
// does not.
|
||||||
|
|
||||||
|
#![feature(match_default_bindings)]
|
||||||
|
#![feature(nll)]
|
||||||
|
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
enum Foo {
|
||||||
|
Base(usize),
|
||||||
|
Next(Rc<Foo>),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_base(mut x: Rc<Foo>) -> Rc<Foo> {
|
||||||
|
while let Foo::Next(n) = &*x {
|
||||||
|
x = n.clone();
|
||||||
|
}
|
||||||
|
x
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let chain = Rc::new(Foo::Next(Rc::new(Foo::Base(44))));
|
||||||
|
let base = find_base(chain);
|
||||||
|
assert_eq!(&*base, &Foo::Base(44));
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user