rust中的生命周期(三)

rust 复制代码
#[derive(Debug,Copy,Clone)]
struct P{}

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let s = P{};
    let sleep_futures:Vec<_> = (1..=10).map(move |t|{
        tokio::task::spawn_blocking(move||{println!("s:{:?}",s);})
    }).collect();
    for k in sleep_futures{
        k.await;
    }
}

这段代码因为是tokio::task::spawn_blocking开启了多线程,多线程内部对s必须就是有所有权。那么这里tokio::task::spawn_blocking(move||{println!("s:{:?}",s);})有move还是不够的,因为这里是多个线程都有对s的所有权,所以这里必须P是copy和Clone都存在。然后这里move的时候,默认是走的Copy的trait。

这里不加Clone和Copy的trait不行,但是如果自己实现Clone。又不会进入到方法内部,不了解原因。

rust 复制代码
#[derive(Debug,Copy)]
struct P{}

impl Clone for P{
    fn clone(&self) -> Self {
        println!("hello");
         P{}
    }
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let s = P{};
    let sleep_futures:Vec<_> = (1..=10).map(move |t|{
        tokio::task::spawn_blocking(move||{println!("s:{:?}",s);})
    }).collect();
    for k in sleep_futures{
       let _= k.await;
    }
}

但是这里面不会打印hello。

Copy trait 表示一种类型的值可以通过按位复制来进行拷贝,而不是通过移动。对于实现了 Copy 的类型,clone 方法通常不会被调用,因为拷贝操作是通过简单的位复制来完成的。

而且如果是使用一次s。则rust的编译器知道只使用一次。这种情况下,P不用实现Copy

rust 复制代码
#[derive(Debug)]
struct P{
    I:i32
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
    let s = P{I:0};
       let k =  tokio::task::spawn_blocking(move||{println!("s:{:?}",s);});
let _= k.await;
}

但是如果是单线程内部,那可以不用实现copy

rust 复制代码
#[derive(Debug)]
struct P{
    I:i32
}
fn main() {
    let s = P{I:0};
    let sleep_futures:Vec<_> = (1..=10).map(move |t|{
       println!("s:{:?}",s);
    }).collect();
}

可以看出rust的编译器非常智能

包括以下这种,不需要实现copy

rust 复制代码
use futures::future::join_all;
use std::time::Instant;

async fn sleep_ms(start: &Instant, id: u64, duration_ms: u64) {
    std::thread::sleep(std::time::Duration::from_millis(duration_ms));
    println!(
        "future {id} slept for {duration_ms}ms, finished after {}ms",
        start.elapsed().as_millis()
    );
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let start = Instant::now();
    let sleep_futures = (1..=10).map(|t| sleep_ms(&start, t, t * 10));
    join_all(sleep_futures).await;
}
复制代码
async fn sleep_ms(start: P, id: u64, duration_ms: u64) {
    std::thread::sleep(std::time::Duration::from_millis(duration_ms));
    println!(
        "future,{:?} {id} slept for {duration_ms}ms, finished after {:?}ms",
        std::thread::current().id(),start
    );
}
#[derive(Debug)]
struct P{
    I:i32
}
    #[tokio::main(flavor = "current_thread")]
    async fn main() {
        let start = P{ I: 0 };
        let sleep_futures = (1..=10).map( |t| sleep_ms(start, t, t * 10));
        join_all(sleep_futures).await;
    }

报错,原因是:

但是:

rust 复制代码
#[derive(Debug)]
struct P{
    I: i32,
}

fn main() {
    let mut s = P { I: 0 };
    let sleep_futures: Vec<_> = (1..=10).map(move |t| {
        s.I = 100;
        println!("s:{:?}", s); // s 被移动到闭包内部并被使用
    }).collect();
}

使用了move依然不会报错。原因是:

这样就报错了

rust 复制代码
#[derive(Debug)]
struct P{
    I:i32
}
fn main() {
    let mut s = P { I: 0 };
    let mut i = 0;
    while i < 3 {
       i += 1;

        let sleep_futures: Vec<_> = (1..=10).map( move|t| {
            s.I =100;
            println!("s:{:?}", s);

        }).collect();
    }
}

rust 复制代码
#[derive(Debug)]
struct P{
    I:i32
}
fn main() {
    let mut s = P { I: 0 };
    let mut i = 0;
    while i < 3 {
       i += 1;

        let sleep_futures: Vec<_> = (1..=10).map( |t| {
            s.I =100;
            println!("s:{:?}", s);

        }).collect();
    }
}

可以保证不错。看来确实这里的s是当作&mut进行编译的,而不是值的所有权转移

相关推荐
doiito12 小时前
【Agent Harness】Gliding Horse 记忆系统深度剖析:像 CPU 一样思考的 AI 记忆架构
ai·rust·架构设计·系统设计·ai agent
doiito1 天前
【Agent Harness】Gliding Horse 给 Agent OS 装上双曲空间引擎与默克尔树边云同步
ai·rust·架构设计·系统设计·ai agent
doiito2 天前
【Agent Harness】Gliding Horse 本体论系统设计:给 AI Agent 装上“语义大脑”
ai·rust·架构设计·系统设计·ai agent
大卫小东(Sheldon)3 天前
Rust 推荐使用宏而非普通函数的场景
rust
doiito3 天前
【Agent Harness】为什么我把 JSON‑LD “编译成 DAG” 后,整个 Agent 平台立刻聪明了
ai·rust·架构设计·系统设计·ai agent
jump_jump3 天前
为了重玩金庸群侠传,我研究了一下 Ruffle 怎么复活 Flash
游戏·rust·github
星栈4 天前
Dioxus 多页面怎么做:`dioxus-router`、嵌套路由、`Outlet` 和页面组织,一篇给你讲顺
前端·rust·前端框架
Rust研习社6 天前
组合真的优于继承吗?为什么 Rust 和 Go 都拥抱组合舍弃继承?
后端·rust·编程语言
红尘散仙7 天前
想写一个像样的终端 App?试试把 React 的开发体验搬进 Rust TUI
前端·rust