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"] }

相关推荐
AIFQuant几秒前
如何快速接入贵金属期货实时行情 API:python 实战分享
开发语言·python·金融·数据分析·restful
Remember_993几秒前
【数据结构】Java对象比较全解析:从equals到Comparable与Comparator,再到PriorityQueue应用
java·开发语言·数据结构·算法·leetcode·哈希算法
郝学胜-神的一滴3 分钟前
深入浅出网络协议:从OSI七层到TCP/IP五层模型全解析
开发语言·网络·c++·网络协议·tcp/ip·程序人生
夏乌_Wx5 分钟前
练题100天——DAY39:单链表练习题×5
c语言·数据结构·算法·链表
qq_406176147 分钟前
吃透JS异步编程:从回调地狱到Promise/Async-Await全解析
服务器·开发语言·前端·javascript·php
@大迁世界11 分钟前
停止使用 innerHTML:3 种安全渲染 HTML 的替代方案
开发语言·前端·javascript·安全·html
txinyu的博客12 分钟前
布隆过滤器
数据结构·算法·哈希算法
52Hz11814 分钟前
力扣240.搜索二维矩阵II、160.相交链表、206.反转链表
python·算法·leetcode
jun_bai16 分钟前
conda环境配置nnU-Net生物医学图像分割肺动脉静脉血管
开发语言·python
程序员zgh22 分钟前
C语言 弱定义机制 解读
c语言·开发语言·c++