To answer this kind of question, “IDE”s like rust-analyzer can be super useful. If you have code like
use std::sync::mpsc;
// just an example
pub struct UserData {
pub guard1: bool,
}
pub struct Message<T> {
pub ev: u8,
pub some_data: T,
}
#[allow(unused)]
fn main() {
let (tx, rx) = mpsc::channel();
let mut msg = Message {ev: 0, some_data: true};
tx.send(msg);
}
it can tell you type information in the tooltip
and if you’re unsure what this “Sender” type is, there’s even a tooltip in the tooltip:
Alternatively, in order to display inferred types without IDEs, it can help to utilize compiler error messages. You say you want to write a function that takes tx
. So just write it with some wrong type like ()
, and see the result:
#![allow(unused)]
use std::sync::mpsc;
// just an example
pub struct UserData {
pub guard1: bool,
}
pub struct Message<T> {
pub ev: u8,
pub some_data: T,
}
fn main() {
let (tx, rx) = mpsc::channel();
let mut msg = Message {ev: 0, some_data: true};
tx.send(msg);
take_tx(tx);
}
fn take_tx(tx: ()) {}
error[E0308]: mismatched types
--> src/main.rs:19:13
|
19 | take_tx(tx);
| ------- ^^ expected `()`, found struct `Sender`
| |
| arguments to this function are incorrect
|
= note: expected unit type `()`
found struct `Sender<Message<bool>>`
note: function defined here
--> src/main.rs:22:4
|
22 | fn take_tx(tx: ()) {}
| ^^^^^^^ ------
notice how it tells you that tx
is a Sender<Message<bool>>
. A similar approach is to instead of calling a function, just assign a variable
let _: () = tx;
error[E0308]: mismatched types
--> src/main.rs:19:17
|
19 | let _: () = tx;
| -- ^^ expected `()`, found struct `Sender`
| |
| expected due to this
|
= note: expected unit type `()`
found struct `Sender<Message<bool>>`