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 小时前
算法基础——一致性
大数据·算法·一致性
敲上瘾1 小时前
BFS(广度优先搜索)——搜索算法
数据结构·c++·算法·搜索引擎·宽度优先·图搜索算法
好好学Java吖2 小时前
【二分题目】
java·开发语言
Captain823Jack2 小时前
【leetcode练习·二叉树拓展】归并排序详解及应用
算法·leetcode·职场和发展
查理零世2 小时前
【算法】回溯算法专题② ——组合型回溯 + 剪枝 python
python·算法·剪枝
米码收割机2 小时前
【PHP】基于 PHP 的图片管理系统(源码+论文+数据库+图集)【独一无二】
开发语言·数据库·php
yyytucj2 小时前
优化 PHP-FPM 参数配置:实现服务器性能提升
服务器·开发语言·php
鲤籽鲲2 小时前
C# 中 [MethodImpl(MethodImplOptions.Synchronized)] 的使用详解
java·开发语言·c#
SomeB1oody2 小时前
【Rust自学】19.5. 高级类型
开发语言·后端·设计模式·rust
逆风局?2 小时前
Java基础——分层解耦——IOC和DI入门
java·开发语言