为什么我不能借用盒装矢量内容作为可变内容? [英] Why can I not borrow a boxed vector content as mutable?

查看:17
本文介绍了为什么我不能借用盒装矢量内容作为可变内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能编译:

fn main() {
    let mut b = Box::new(Vec::new());
    b.push(Vec::new());
    b.get_mut(0).unwrap().push(1);
}

虽然这样做:

fn main() {
    let a = Box::new(Vec::new());
    let mut b = *a;
    b.push(Vec::new());
    b.get_mut(0).unwrap().push(1);
}

这也是:

fn main() {
    let mut b = Vec::new();
    b.push(Vec::new());
    b.get_mut(0).unwrap().push(Vec::new());
    b.get_mut(0).unwrap().get_mut(0).unwrap().push(1)
}

对我来说第一个和第三个在概念上是相同的 - Vector of Vectors of integer 和一个 Boxcode>Vector of Vector of Vectors of integers,但最后一个导致每个向量都是可变的,而第一个导致内部向量不可变.

The first and third one for me are the conceptually the same - Box of a Vector of Vectors of integers and a Vector of Vector of Vectors of integers, but the last one results in each vector being mutable, whereas the first one makes the inner vector immutable.

推荐答案

至少在 Rust 1.25.0 中,所有三个原始示例都有效.这是 Rust 以前版本中的一个错误.

In at least Rust 1.25.0, all three original examples work. This was a bug in some previous version of Rust.

这篇关于为什么我不能借用盒装矢量内容作为可变内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆