spawn — 새 스레드 생성

spawn — 새 스레드 생성

spawn는 새 스레드를 생성하는 함수예요.

출처: Rust 공식 문서

본문

pub fn spawn<F, T>(f: F) -> JoinHandle<T>
where
    F: FnOnce() -> T + Send + 'static,
    T: Send + 'static,

새 스레드에서 클로저 f를 실행하고, 그 스레드를 조인할 수 있는 JoinHandle<T>를 반환해요. 기본 설정의 thread::Builder를 사용하며, 실패 시 unwrap해요(즉 실패하면 패닉).

use std::thread;

let handle = thread::spawn(|| {
    println!("Hello from a thread!");
});
handle.join().unwrap();

더 알아보기 (Learn more)