- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个方法,该方法返回一个遍历 HashMap
值的迭代器装在 RefCell
内,但我在 Ref
处遇到错误由 RefCell::borrow
返回没有足够长的时间从方法返回迭代器。这是我的代码:
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::hash_map::Values;
struct Foo {
map: Rc<RefCell<HashMap<i32, i32>>>,
}
impl Foo {
fn iter(&self) -> Values<i32, i32> {
self.map.borrow().values()
}
}
fn main() {
let foo = Foo {
map: Rc::new(RefCell::new(HashMap::new()))
};
for v in foo.iter() {
println!("{}", v)
}
}
编译错误:
rustc 1.15.1 (021bd294c 2017-02-08)
error: borrowed value does not live long enough
--> <anon>:12:9
|
12 | self.map.borrow().values()
| ^^^^^^^^^^^^^^^^^ does not live long enough
13 | }
| - temporary value only lives until here
|
How do I return a reference to something inside a RefCell without breaking encapsulation?建议创建一个封装 Ref
的守卫并提供一个用于访问底层值的接口(interface),但我需要做的是返回一个迭代器对象 ( Values<'a, K, V>
),它已经封装了对 HashMap
的简单引用。 .
我的主要问题是我有一个运行时跟踪引用 Ref<T>
而我需要一个简单的引用来创建一个迭代器。 Ref::map
公开了一个用于映射的普通引用,但它需要映射器函数返回另一个在这里不可能的引用。我是否应该重做整个迭代器功能以使用 Ref
或者有更好的方法吗?
最佳答案
你不能这样做。
最终的问题是 std::collections::hash_map::Values
持有一个引用,但你没有“只是”一个引用。你有智能指针 Ref
。
我所知道的最简单的解决方案是反转代码:
impl Foo {
fn with_iter<F, T>(&self, f: F) -> T
where
F: FnOnce(Values<i32, i32>) -> T,
{
f(self.map.borrow().values())
}
}
fn main() {
let foo = Foo {
map: Rc::new(RefCell::new(HashMap::new())),
};
foo.with_iter(|i| {
for v in i {
println!("{}", v)
}
})
}
在这里,Values
迭代器不再需要比 borrow
的结果长寿,因此没有额外的复杂性。
如果您愿意泄露您的实现,您可以返回 Ref
:
impl Foo {
fn iter(&self) -> Ref<'_, HashMap<i32, i32>> {
self.map.borrow()
}
}
for v in foo.iter().values() {
println!("{}", v)
}
在较新版本的 Rust 中,您可以返回一个实现了 Deref
的未命名类型:
use std::ops::Deref;
impl Foo {
fn iter(&self) -> impl Deref<Target = HashMap<i32, i32>> + '_ {
self.map.borrow()
}
}
另见:
关于reference - 我如何返回一个引用了 RefCell 中某些内容的迭代器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58383014/
Rust 文档涵盖 Rc>相当广泛,但不涉及 RefCell> ,我现在遇到了。 这些是否有效地给出了相同的结果?它们之间有重要区别吗? 最佳答案 Do these effectively give
Rust 文档涵盖 Rc>相当广泛,但不涉及 RefCell> ,我现在遇到了。 这些是否有效地给出了相同的结果?它们之间有重要区别吗? 最佳答案 Do these effectively give
如果我做对了,就不可能通过 std::rc::Rc 创建一个可变的借用。在 Rust 中,你必须使用 Cell或 RefCell .但无论如何我无法理解如何使用它们。例如考虑这个简单的 example
我正在尝试转换 Rc>至 Rc> ( Data 实现 Interface )但在通用方法中是不可能的: use std::cell::RefCell; use std::rc::Rc; trait I
我最初问过这个问题 here , 但它被标记为重复,尽管在我看来它只复制了一部分,所以我创建了一个更具体的: 考虑以下代码: use std::rc::Rc; trait MyTrait {
我正在实现VirtualTapInterface的特征。此特征的receive函数应创建一个TxToken结构,其中lower属性必须是包含当前Rc>的VirtualTapInterface,即sel
我不知道如何在以下代码片段中实现 children_iter: use std::{cell::RefCell, rc::Rc};
我有一段代码需要对列表进行操作。此列表包含来自其他来源并需要处理并最终删除的项目。该列表还传递给多个函数,这些函数决定是添加还是删除项目。我创建了一个示例代码来反射(reflect)我的问题: use
是否可以创建类型为 RefCell 的东西?在使用rust ?我尝试了以下方法: fn test2(x : T) -> RefCell{ return RefCell::new(x) as R
背景 我正在编写一个库,一些相关的代码如下所示: pub struct MyStruct1; pub struct MyStruct2; pub enum MyEnum { Variant1(
这个问题在这里已经有了答案: How do I return a reference to something inside a RefCell without breaking encapsulat
我正在使用Rc>(Playground)实现二进制搜索树 use std::cell::RefCell; use std::rc::Rc; type RcRefBaseNode = Rc>>; typ
我一直在思考为什么 Rust 中的内部可变性在大多数情况下需要运行时检查(例如 RefCell )。看起来我找到了一个没有运行时成本的安全替代方案。我调用类型 SafeCell (主要是因为它是围绕
为什么我们不能将 RefCell 用于 Rust 中的递归数据结构? 无效: enum List { Cons(i32, RefCell), Nil, } 有效: enum List
我有一个包含两个 Vec 的结构,它们被包裹在 RefCell 中。我想在该结构上有一个方法,它结合了两个向量并将它们作为新的 RefCell 或 RefMut 返回: use std::cell::
我正在尝试创建一个方法,该方法返回一个遍历 HashMap 值的迭代器装在 RefCell 内,但我在 Ref 处遇到错误由 RefCell::borrow 返回没有足够长的时间从方法返回迭代器。这是
我试图将 RefCell 传递给闭包中的函数,然后从闭包内部修改同一个变量。这是我的代码: let path: Rc>>> = Rc::new(RefCell::new(None)); ... //v
我有一个包含两个 C 指针和一个 Rust 指针的结构 HashMap . struct MyStruct { p1: *mut ..., p2: *mut ..., hm:
我有一个链表类型的结构,使用 Option、Rc 和 RefCell。 我想为它实现 fmt::Debug,但遇到了可爱的“无法移出借用的内容”错误。 use std::fmt; use std::r
我有一个具有内部可变性的结构。 use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec, } s
我是一名优秀的程序员,十分优秀!