forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rust-lang#65 from oli-obk/master
small fixes
- Loading branch information
Showing
7 changed files
with
208 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2015 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. | ||
|
||
//error-pattern: no mir for `std::panicking::panicking` | ||
|
||
use std::sync::Mutex; | ||
|
||
fn par_for<I, F>(iter: I, f: F) | ||
where I: Iterator, | ||
I::Item: Send, | ||
F: Fn(I::Item) + Sync | ||
{ | ||
for item in iter { | ||
f(item) | ||
} | ||
} | ||
|
||
fn sum(x: &[i32]) { | ||
let sum_lengths = Mutex::new(0); | ||
par_for(x.windows(4), |x| { | ||
*sum_lengths.lock().unwrap() += x.len() | ||
}); | ||
|
||
assert_eq!(*sum_lengths.lock().unwrap(), (x.len() - 3) * 4); | ||
} | ||
|
||
fn main() { | ||
let mut elements = [0; 20]; | ||
|
||
// iterators over references into this stack frame | ||
par_for(elements.iter_mut().enumerate(), |(i, x)| { | ||
*x = i as i32 | ||
}); | ||
|
||
sum(&elements) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2014 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(slice_patterns)] | ||
|
||
fn main() { | ||
let mut x: &[_] = &[1, 2, 3, 4]; | ||
|
||
let mut result = vec!(); | ||
loop { | ||
x = match *x { | ||
[1, n, 3, ref rest..] => { | ||
result.push(n); | ||
rest | ||
} | ||
[n, ref rest..] => { | ||
result.push(n); | ||
rest | ||
} | ||
[] => | ||
break | ||
} | ||
} | ||
assert_eq!(result, [2, 4]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2014 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(slice_patterns)] | ||
|
||
fn main() { | ||
assert_eq!(match [0u8; 1024] { | ||
_ => 42_usize, | ||
}, 42_usize); | ||
|
||
assert_eq!(match [0u8; 1024] { | ||
[1, _..] => 0_usize, | ||
[0, _..] => 1_usize, | ||
_ => 2_usize | ||
}, 1_usize); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2014 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(advanced_slice_patterns)] | ||
#![feature(slice_patterns)] | ||
|
||
use std::fmt::Debug; | ||
|
||
fn foldl<T, U, F>(values: &[T], | ||
initial: U, | ||
mut function: F) | ||
-> U where | ||
U: Clone+Debug, T:Debug, | ||
F: FnMut(U, &T) -> U, | ||
{ match values { | ||
&[ref head, ref tail..] => | ||
foldl(tail, function(initial, head), function), | ||
&[] => { | ||
// FIXME: call guards | ||
let res = initial.clone(); res | ||
} | ||
} | ||
} | ||
|
||
fn foldr<T, U, F>(values: &[T], | ||
initial: U, | ||
mut function: F) | ||
-> U where | ||
U: Clone, | ||
F: FnMut(&T, U) -> U, | ||
{ | ||
match values { | ||
&[ref head.., ref tail] => | ||
foldr(head, function(tail, initial), function), | ||
&[] => { | ||
// FIXME: call guards | ||
let res = initial.clone(); res | ||
} | ||
} | ||
} | ||
|
||
pub fn main() { | ||
let x = &[1, 2, 3, 4, 5]; | ||
|
||
let product = foldl(x, 1, |a, b| a * *b); | ||
assert_eq!(product, 120); | ||
|
||
let sum = foldr(x, 0, |a, b| *a + b); | ||
assert_eq!(sum, 15); | ||
} |