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

相关推荐
CoderCodingNo1 分钟前
【GESP】C++八级考试大纲知识点梳理 (5) 代数与平面几何
开发语言·c++
爱喝白开水a7 分钟前
春节后普通程序员如何“丝滑”跨行AI:不啃算法,也能拿走AI
java·人工智能·算法·spring·ai·前端框架·大模型
毕设源码-朱学姐11 分钟前
【开题答辩全过程】以 基于Java的运动场地预约系统为例,包含答辩的问题和答案
java·开发语言
chushiyunen22 分钟前
python numpy包的使用
开发语言·python·numpy
小邓睡不饱耶23 分钟前
Python多线程爬虫实战:爬取论坛帖子及评论
开发语言·爬虫·python
张辰宇-23 分钟前
AcWing 5 多重背包问题 II
算法
毕设源码-邱学长25 分钟前
【开题答辩全过程】以 基于 java web 的篮球赛事管理系统的设计与实现为例,包含答辩的问题和答案
java·开发语言
zhangren0246835 分钟前
PHP vs C++:从Web脚本到系统编程的终极对比
开发语言·c++·php
聆风吟º37 分钟前
【C标准库】深入理解 C 语言memmove函数:安全内存拷贝的利器
c语言·开发语言·memmove·库函数
小则又沐风a38 分钟前
类和对象(C++)---上
java·c++·算法