Type of a Channel Question (Eliding makes examples scarce...)

#1

Stupid question time…

I want to use a crossbeam_channel to send data between threads, I know the type of the data I want to send, but can’t figure out how to instantiate the channel and specify what kind of data to expect.

The examples I can find all infer the type based on what type is sent through it. But I haven’t written that code yet.

error[E0282]: type annotations needed
  --> src/main.rs:34:34
   |
34 |     let (a_sender, a_receiver) = crossbeam_channel::unbounded();
   |         ----------------------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `T`
   |         |
   |         consider giving the pattern a type

error: aborting due to previous error

Thanks,

-kb

0 Likes

#2

Two ways, either specify the type as the error suggests:

let (tx, rx): (Sender<T>, Receiver<T>) = unbounded();

or specify the generic type in the function call:

let (tx, rx) = unbounded::<T>();
2 Likes

#3

Thank you!

-kb, the Kent whose fingers are still learning the language, and when he drops it for a few weeks they forget what they knew.

0 Likes

#4

By the way, the function::<T>() syntax is called the turbo fish.

1 Like