Passing parameters to a thread function


Dear Gurus, please help me to find out if it is possible to pass parameters for thread function.
For example: // C++ code
void func(int x,int y)
void main()
std::thread Ex=new std::thread(func,5,5);

I have searched the enet and havent found any information on passing parameters to thread function in Rust.

let the_text = "abc";
let thread = ::std::thread::spawn(|| {
    println!("The text is: {}", the_text);


But here is another question:
Do i always have to spawn the closures???
Because i tried to spawn a fn and it worked:
fn func()
println!(“Thread function”);
fn main(){
let thr=std::thread::spawn(func);
and it also works.
I think it is pretty uncomfortable to describe a closure with parameters in a spawn() method.
Is there a way in Rust to pass a fn and its parameters to operate with in spawn method ???


No. you’re not required to always use closures.

The definition of std::thread::spawn() is as follows:

pub fn spawn<F, T>(f: F) -> JoinHandle<T> 
    F: FnOnce() -> T,
    F: Send + 'static,
    T: Send + 'static, 

The argument you’re passing, f, is the function. The function, when called, should return a T, which you then receive wrapped in a JoinHandle.

See the doc page for std::thread::spawn() and std::marker::Send for further details. In general, you’ll be able to pass it a function most of the time.


Could you please give me an example of spawn() with parameters for thread method that is passing to spawn(). ???
For example:
fn func(x:i32,y:i32)

fn main()
std::thread::spawn(func, ???);
I just want to know how to pass parameters to a thread function that i pass to spawn() ???


The closure given to spawn is more like a C++ callable, rather than a function pointer. Your example would be:

fn func(x: i32, y: i32) {
    println!("{}", x + y);

fn main() {
    let h = std::thread::spawn(|| func(5,5));

std::thread::spawn(|| func(1, 2));

There is no other way to pass parameters. It would require some kind of variable generics, which Rust doesn’t have.


Dear Vitalyd,
Thanks very much.
To be honest i turned out to be dummy not to guess that i can call a function with params in a closure, even though i practiced calling fns in a closure.


Another thing i want to ask is:
Is it possible to declare a fn that accept a Macros as one of parameter. If it is what is the syntax then. I havent managed to find any information about it anywhere so far.


Macros only happen at parse time; they’re gone by runtime, so cannot be passed to a function.

(Similarly, if you #define MAX(a,b) ((a)>(b)?(a):(b)) in C++ you can’t pass MAX to a function.)


Thank you Sir.
Does it mean that there is neither reference nor pointer to a Macros because it cannot be interpreted as Type, right ??


That’s correct. At runtime, there’s nothing left of macros.


BTW, if you put your code in ``` It will get pretty printed into a formatted code block. Add the name of the language and you may get coloring as well.


Dear Pros. Please help to figure out what to do to use @Type and ~Type.
Also it would be great to get any ref to info about working with files using Rust especially using traits ReaderUtil and WriterUtil.


I suspect you’re looking at some old version of Rust. What exactly do you want to do?

Also, please start a new thread if you’re switching gears.


Sir I want to use Pointers to @ and ~ blocks of memory as is written in an article. But using such syntax doesnt work, nor works the code writing and reading in and from the file.



Right - the ~ and @ modifiers don’t exist in Rust today - you’re looking at a very old article on Rust. Please start a new topic/thread to discuss whatever question you have on reading/writing from/to files.


Dear fellows.
Could you please help to find out how can i pass a mutable reference to objects among the threads.
I want to change values of vector in another thread for example,
the book says that i can use Arc but i cant emagine the right syntax.


let mut ptd=Arc::new(vec![5,6,7,8]);
thread::spawn(move||{ for i in 0…ptd.len(){

error[E0596]: cannot borrow immutable borrowed content as mutable
| ^^^ cannot borrow as mutable
thread::spawn(move||{ for i in 0…ptd.len(){
| ------ value moved (into closure) here

33 | for i in 0…ptd.len(){
| ^^^ value used here after move