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

相关推荐
yaoxin5211231 分钟前
85. Java Record 深入解析:构造函数、访问器、序列化与实际应用
java·开发语言
十五年专注C++开发6 分钟前
CMake指令:add_library()
开发语言·c++·cmake·自动化编译工具
白熊18810 分钟前
【图像大模型】AnimateDiff:基于扩散模型的视频生成技术解析与实践指南
人工智能·算法·音视频
几个几个n11 分钟前
Matlab入门
开发语言·人工智能·matlab
Evand J17 分钟前
MATLAB例程——基于分批运输与最近邻优化的垃圾运输路径规划,n个垃圾收集点,每点有固定垃圾量,车辆从处理厂出发收集垃圾后返回,目标是最小化总行驶距离
开发语言·matlab
@老蝴26 分钟前
C语言 — 内存函数和数据的存储
c语言·开发语言
编码小笨猪34 分钟前
简说Qt信号和槽
开发语言·qt
少可爱1 小时前
对接钉钉消息样例:DING消息、机器人
java·开发语言·钉钉
十五年专注C++开发1 小时前
CMake基础:CMakeLists.txt 文件结构和语法
开发语言·c++·cmake·跨平台编译
虾球xz1 小时前
游戏引擎学习第309天:用于重叠检测的网格划分
c++·学习·算法·游戏引擎