ADVERTISEMENT
ADVERTISEMENT

Rust program to set the name of a thread

Rust | Thread Example: Write a program to set the name of a thread.
Submitted by Nidhi, on November 06, 2021

Problem Solution:

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

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".

Rust Threads Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.