rust/src/test/run-pass/sendfn-spawn-with-fn-arg.rs

31 lines
870 B
Rust
Raw Normal View History

// Copyright 2012 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.
use std::cell::Cell;
use std::task;
2013-04-26 14:04:39 -07:00
pub fn main() { test05(); }
2011-12-15 16:10:01 -08:00
2013-05-07 21:33:31 -07:00
fn test05_start(f: ~fn(int)) {
2011-12-15 16:10:01 -08:00
f(22);
}
fn test05() {
let three = ~3;
let fn_to_send: ~fn(int) = |n| {
2013-09-29 19:23:57 -07:00
error2!("{}", *three + n); // will copy x into the closure
assert_eq!(*three, 3);
2011-12-15 16:10:01 -08:00
};
let fn_to_send = Cell::new(fn_to_send);
task::spawn(|| {
2013-04-26 14:04:39 -07:00
test05_start(fn_to_send.take());
2012-01-04 21:14:53 -08:00
});
2011-12-15 16:10:01 -08:00
}