Question about channel


#1

It’s possible create a function with one parameter of channel()?

let (tx, rx) = channel();
calc_something(rx);

If yes, how may I do that?


#2

It should work fine: define your function to takes the type of whichever endpoint you wish (sender or receiver). Are you having difficulties/compiler errors with specific code you’ve tried?


#3

I try what you said, but still not working. I got this error “wrong number of type arguments: expected 1, found 0 [E0243]”

use std::sync::mpsc::Receiver; fn fn send_data(&mut self, rx: Receiver) -> () { }


#4

How are you calling it? That definition looks fine, but the error is for the call site.


#5

Receiver is a generic type; it has to know the type of data you’re sending across. Example:

fn send_data(&mut self, rx: Receiver<String>) -> ()

Depending on what exactly you want to do, you can also make send_data itself generic, with

fn send_data<T>(&mut self, rx: Receiver<T>) -> ()

#6

It works !
Thanks =)