Channels

๋Ÿฌ์ŠคํŠธ์˜ ์ฑ„๋„์€ Sender ์™€Receiver` ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ถ€๋ถ„์€ ์ฑ„๋„์„ ํ†ตํ•ด ์—ฐ๊ฒฐ๋˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ์ข…๋‹จ๋งŒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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.

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

์—ญ์ฃผ

  • ๋ฉ”์„ธ์ง€ ํŒจ์‹ฑํ•˜๋Š” transmitter(tx)์™€ receiver(rx) ์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต ์˜ˆ์‹œ๋Š” ํ๋ฅด๋Š” ๊ฐœ์šธ(์ฑ„๋„) ์ƒ๋ฅ˜(tx)์— ๋„์šด ๊ณ ๋ฌด ์˜ค๋ฆฌ๊ฐ€ ํ•˜๋ฅ˜(rx)๋กœ ๊ฐ€๋Š” ํ๋ฆ„์„ ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.