rust way step 7

rust 复制代码
use std::{sync::mpsc, thread, time::Duration};

fn spawn_function() {
    for i in 0..5 {
        println!("spawned thread print {}", i);
        thread::sleep(Duration::from_millis(1));
    }
}

#[tokio::main]
async fn main() {
    thread::spawn(spawn_function);


    thread::spawn(|| {
        for i in 0..5 {
            println!("spawned thread printa {}", i);
            thread::sleep(Duration::from_millis(1));
        }
    });

    





    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let val = String::from("hi............");
        tx.send(val).unwrap();
    });

    let received = rx.recv().unwrap();
    println!("Got: {}", received);



    let s = "hello";
    
    let handle = thread::spawn(move || {
        println!("{}", s);
    });

    handle.join().unwrap();
    thread::sleep(Duration::from_secs(1));


    /*************************************************************************/
    async fn async_task() -> u32 {
       
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;
      
       return   999;
    }
    
    // 异步任务执行函数
    async fn execute_async_task() {
        // 调用异步任务,并等待其完成
        let result = async_task().await;
        // 输出结果
        println!("Async task result: {}", result);
    }
    execute_async_task().await;

    println!("Async task completed!");    


    async fn hello() -> String {
        "Hello, world!".to_string()
    }
    async fn print_hello() {
        let result = hello().await;
        println!("{}", result);
    }

    print_hello().await;

    thread::sleep(Duration::from_secs(2));
}

tokio = { version = "1.0.0", features = ["full"] }

相关推荐
liu****1 天前
3.链表讲解
c语言·开发语言·数据结构·算法·链表
小灰灰搞电子1 天前
Rust 动态分发(dyn Trait)详解
开发语言·后端·rust
第二只羽毛1 天前
C++ 高性能编程要点
大数据·开发语言·c++·算法
老华带你飞1 天前
旅游|基于Java旅游信息系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·旅游
CQ_YM1 天前
数据结构之栈
数据结构·算法·
爱学习的梵高先生1 天前
C++:基础知识
开发语言·c++·算法
oioihoii1 天前
C++对象生命周期与析构顺序深度解析
java·开发语言·c++
IMPYLH1 天前
Lua 的 tonumber 函数
开发语言·笔记·后端·junit·游戏引擎·lua
xlq223221 天前
24.map set(下)
数据结构·c++·算法
It's now1 天前
BeanRegistrar 的企业级应用场景及最佳实践
java·开发语言·spring