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

相关推荐
疋瓞1 天前
C++_STL和数据结构《1》_STL、STL_迭代器、c++中的模版、STL_vecto、列表初始化、三个算法、链表
数据结构·c++·算法
JJJJ_iii1 天前
【左程云算法09】栈的入门题目-最小栈
java·开发语言·数据结构·算法·时间复杂度
枫叶丹41 天前
【Qt开发】显示类控件(三)-> QProgressBar
开发语言·qt
Bear on Toilet1 天前
继承类模板:函数未在模板定义上下文中声明,只能通过实例化上下文中参数相关的查找找到
开发语言·javascript·c++·算法·继承
金融小师妹1 天前
多因子AI回归揭示通胀-就业背离,黄金价格稳态区间的时序建模
大数据·人工智能·算法
程序员东岸1 天前
C语言入门指南:字符函数和字符串函数
c语言·笔记·学习·程序人生·算法
码猿宝宝1 天前
浏览器中javascript时间线,从加载到执行
开发语言·javascript·ecmascript
OEC小胖胖1 天前
App Router vs. Pages Router:我应该如何选择?
开发语言·前端·前端框架·web·next.js
小猪咪piggy1 天前
【算法】day2 双指针+滑动窗口
数据结构·算法·leetcode
max5006001 天前
OpenSTL PredRNNv2 模型复现与自定义数据集训练
开发语言·人工智能·python·深度学习·算法