destroyed when this function returns } fn main() { let file = File::open("hello.txt").unwrap(); do_stuff_with_file(file); // ownership of file is moved into `do_stuff_with_file` do_more_stuff_with_file(file); // ERROR: Use of moved value `file` }
... // f is borrowed, and therefore not destroyed } fn main() { let file = File::open("hello.txt").unwrap(); do_stuff_with_file(&file); // do_stuff_with_file borrows `file`, ownership is not moved. do_more_stuff_with_file(file); // This works fine now. }
one thread to another • Any data which is made only of Send values is automatically Send • Types which can be used for sharing data are not Send (like pointers).
make a closure be a struct, it'd look something like this: struct Closure { args: Args, env: Environment, } struct Args { # field for each argument type } struct Environment { # field for each value closed over }
give you a shared (immutable) reference to it's data. • The Arc owns the value, no way to reclaim ownership. • Need a type that is Sync and also exposes a mutable reference to it's data
types that give mutability and are Sync • The only way to share data across threads (in safe Rust anyway) • The primitives Mutex uses are still accessible but your type would not be Sync or Send by default