mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-08 18:46:15 +02:00
24 lines
503 B
Markdown
24 lines
503 B
Markdown
|
# Channels
|
||
|
|
||
|
Rust channels have two parts: a `Sender<T>` and a `Receiver<T>`. The two parts
|
||
|
are connected via the channel, but you only see the end-points.
|
||
|
|
||
|
```rust,editable
|
||
|
use std::sync::mpsc;
|
||
|
use std::thread;
|
||
|
|
||
|
fn main() {
|
||
|
let (tx, rx) = mpsc::channel();
|
||
|
|
||
|
tx.send(10).unwrap();
|
||
|
tx.send(20).unwrap();
|
||
|
|
||
|
println!("Received: {:?}", rx.recv());
|
||
|
println!("Received: {:?}", rx.recv());
|
||
|
|
||
|
let tx2 = tx.clone();
|
||
|
tx2.send(30).unwrap();
|
||
|
println!("Received: {:?}", rx.recv());
|
||
|
}
|
||
|
```
|