Q:

Rust program to set the name of a thread

0

In this program, we will create a thread by assigning the name of the thread using the thread::Builder::new().name() method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to set the name of a thread is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to set the name 
// of a thread

use std::thread;
use std::time::Duration;

fn main() {
	thread::Builder::new().name("MyThread".to_string()).spawn(move || {
		println!("Thread is running");
	});	
   
	thread::sleep(Duration::from_millis(500));
	println!("Program finished");
}

Output:

$ rustc main.rs

$ ./main
Thread is running
Program finished

Explanation:

Here, we created a thread by assigning the name MyThread using the thread::Builder::new().name() method and printed the message "Thread is running".

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now