Question about channel


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

let (tx, rx) = channel();

If yes, how may I do that?


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?


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) -> () { }


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


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>) -> ()


It works !
Thanks =)