The following steps will walk you through this implementation:
- Create a file named sample_move.rs and open it in your text editor.
- Write the code header with the relevant information:
//-- #########################
//-- Task: Passing values to a thread in rust
//-- Author: Vigneshwer.D
//-- Version: 1.0.0
//-- Date: 19 March 17
//-- #########################
- Call the standard thread library using the use keyword:
use std::thread;
- Define the main function and declare two variables: x and handle. Assign x with the integer value 1 and assign handle to the new thread, using the thread::spawn command. Here's the code for this:
fn main() {
let x = 1; let handle = thread::spawn(move || {(x)});
println!("{:?}", handle.join().unwrap());
}
You will get the following screenshot as an output upon successful execution of the code: