From 537889aa78c984ee6484d16fec4a67f35778aec6 Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Wed, 21 Jan 2015 11:16:00 -0800 Subject: [PATCH] Fix type inference problems in tests and docs --- src/doc/intro.md | 8 +++---- src/doc/trpl/looping.md | 2 +- src/doc/trpl/threads.md | 4 ++-- src/test/bench/shootout-binarytrees.rs | 32 ++++++++++++++------------ 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/doc/intro.md b/src/doc/intro.md index a7c37ba8e07e..b92d38215c29 100644 --- a/src/doc/intro.md +++ b/src/doc/intro.md @@ -480,14 +480,12 @@ use std::sync::{Arc,Mutex}; fn main() { let numbers = Arc::new(Mutex::new(vec![1is, 2, 3])); - for i in 0..3 { + for i in 0us..3 { let number = numbers.clone(); Thread::spawn(move || { let mut array = number.lock().unwrap(); - - array[i as usize] += 1; - - println!("numbers[{}] is {}", i, array[i as usize]); + array[i] += 1; + println!("numbers[{}] is {}", i, array[i]); }); } } diff --git a/src/doc/trpl/looping.md b/src/doc/trpl/looping.md index 28f02b1ffe15..4301149d1f8b 100644 --- a/src/doc/trpl/looping.md +++ b/src/doc/trpl/looping.md @@ -123,7 +123,7 @@ We now loop forever with `loop` and use `break` to break out early. iteration. This will only print the odd numbers: ```{rust} -for x in 0..10 { +for x in 0u32..10 { if x % 2 == 0 { continue; } println!("{}", x); diff --git a/src/doc/trpl/threads.md b/src/doc/trpl/threads.md index 1bad09b4b6e5..a801a1ab0e92 100644 --- a/src/doc/trpl/threads.md +++ b/src/doc/trpl/threads.md @@ -179,7 +179,7 @@ for init_val in 0 .. 3 { } let result = rx.recv().unwrap() + rx.recv().unwrap() + rx.recv().unwrap(); -# fn some_expensive_computation(_i: u32) -> u32 { 42 } +# fn some_expensive_computation(_i: i32) -> i32 { 42 } ``` Cloning a `Sender` produces a new handle to the same channel, allowing multiple @@ -207,7 +207,7 @@ let rxs = (0 .. 3).map(|&:init_val| { // Wait on each port, accumulating the results let result = rxs.iter().fold(0, |&:accum, rx| accum + rx.recv().unwrap() ); -# fn some_expensive_computation(_i: u32) -> u32 { 42 } +# fn some_expensive_computation(_i: i32) -> i32 { 42 } ``` ## Backgrounding computations: Futures diff --git a/src/test/bench/shootout-binarytrees.rs b/src/test/bench/shootout-binarytrees.rs index 58e2430b0ffa..4182f8b651b2 100644 --- a/src/test/bench/shootout-binarytrees.rs +++ b/src/test/bench/shootout-binarytrees.rs @@ -41,7 +41,7 @@ extern crate arena; use std::iter::range_step; -use std::thread::Thread; +use std::thread::{Thread, JoinGuard}; use arena::TypedArena; struct Tree<'a> { @@ -71,6 +71,18 @@ fn bottom_up_tree<'r>(arena: &'r TypedArena>, item: i32, depth: i32) } } +fn inner(depth: i32, iterations: i32) -> String { + let mut chk = 0; + for i in 1 .. iterations + 1 { + let arena = TypedArena::new(); + let a = bottom_up_tree(&arena, i, depth); + let b = bottom_up_tree(&arena, -i, depth); + chk += item_check(&a) + item_check(&b); + } + format!("{}\t trees of depth {}\t check: {}", + iterations * 2, depth, chk) +} + fn main() { let args = std::os::args(); let args = args.as_slice(); @@ -97,20 +109,10 @@ fn main() { let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth); let messages = range_step(min_depth, max_depth + 1, 2).map(|depth| { - use std::num::Int; - let iterations = 2.pow((max_depth - depth + min_depth) as usize); - Thread::scoped(move|| { - let mut chk = 0; - for i in 1 .. iterations + 1 { - let arena = TypedArena::new(); - let a = bottom_up_tree(&arena, i, depth); - let b = bottom_up_tree(&arena, -i, depth); - chk += item_check(&a) + item_check(&b); - } - format!("{}\t trees of depth {}\t check: {}", - iterations * 2, depth, chk) - }) - }).collect::>(); + use std::num::Int; + let iterations = 2.pow((max_depth - depth + min_depth) as usize); + Thread::scoped(move || inner(depth, iterations)) + }).collect::>(); for message in messages.into_iter() { println!("{}", message.join().ok().unwrap());