相关疑难解决方法(0)

当我可以使用Cell或RefCell时,我应该选择哪个?

std::cell文档中,我看到Cell"只与实现的类型兼容Copy".这意味着我必须使用RefCellCopy类型.

当我这样做有一个Copy类型,是否有使用一种类型的细胞在另一个好处?我假设答案是肯定的,因为否则两种类型都不存在!使用一种类型而不是另一种类型有什么好处和权衡?

这是一个愚蠢的,虚构的例子,使用两者CellRefCell实现相同的目标:

use std::cell::{Cell,RefCell};

struct ThingWithCell {
    counter: Cell<u8>,
}

impl ThingWithCell {
    fn new() -> ThingWithCell {
        ThingWithCell { counter: Cell::new(0) }
    }

    fn increment(&self) {
        self.counter.set(self.counter.get() + 1);
    }

    fn count(&self) -> u8 { self.counter.get() }
}

struct ThingWithRefCell {
    counter: RefCell<u8>,
}

impl ThingWithRefCell {
    fn new() -> ThingWithRefCell {
        ThingWithRefCell { counter: RefCell::new(0) }
    }

    fn …
Run Code Online (Sandbox Code Playgroud)

rust

27
推荐指数
3
解决办法
6294
查看次数

Cell或RefCell是最佳选择的情况

您什么时候需要使用Cell或RefCell?似乎有许多其他类型选择适合代替这些,文档警告说使用RefCell是一种"最后的手段".

使用这些类型是" 代码味 "吗?任何人都可以展示一个例子,使用这些类型比使用其他类型更有意义,例如Rc甚至Box

rust interior-mutability

22
推荐指数
3
解决办法
5355
查看次数

将修改其环境的闭包传递给 Rust 中的函数

我有一个捕获并修改其环境的闭包。我想将此闭包传递给接受闭包的函数:

fn main() {
    let mut integer = 5;
    let mut closure_variable = || -> i32 {
        integer += 1;
        integer
    };
    execute_closure(&mut closure_variable);
}

fn execute_closure(closure_argument: &mut Fn() -> i32) {
    let result = closure_argument();
    println!("Result of closure: {}", result);
}
Run Code Online (Sandbox Code Playgroud)

因为闭包修改了它的环境,所以失败了:

fn main() {
    let mut integer = 5;
    let mut closure_variable = || -> i32 {
        integer += 1;
        integer
    };
    execute_closure(&mut closure_variable);
}

fn execute_closure(closure_argument: &mut Fn() -> i32) {
    let result = closure_argument();
    println!("Result of …
Run Code Online (Sandbox Code Playgroud)

closures immutability traits rust

5
推荐指数
1
解决办法
2999
查看次数

闭包中的可变性问题

我真的不知道如何克服这个问题.据我所知,它words被移入封闭(这对我来说很好,它是唯一可以在此之后使用的地方),但需要根据而来typed_some.错误提示听起来像一个体面的想法,只是该部分在一个库中,我不知道它是否是他们可以实现的东西.
on_edit文档.

extern crate cursive;
extern crate rand;

use cursive::Cursive;
use cursive::views::{Dialog, TextView, EditView, LinearLayout};
use cursive::traits::Identifiable;
use rand::Rng;

fn main() {
    // This really messes with stdout. Seems to disable it by default but when
    // siv is running println prints in random places on the screen.
    let mut siv = Cursive::new();
    siv.add_global_callback('q', |s| s.quit());

    let mut words = WordBar::new();

    siv.add_layer(Dialog::around(LinearLayout::vertical()
            .child(TextView::new(words.update_and_get_bar()).with_id("target_field"))
            .child(EditView::new()
                .on_edit(move |s, input, _| words.typed_some(s, input))
                .with_id("input_field")))
        .title("Keyurses")
        .button("Quit", |s| s.quit())); …
Run Code Online (Sandbox Code Playgroud)

closures mutable rust

3
推荐指数
1
解决办法
410
查看次数

如果值超过闭包,在闭包之间共享引用的正确方法是什么?

我想在两个闭包之间共享一个引用;在一个封闭中可变。这是一个人为的情况,但我发现在学习 Rust 的背景下它很有趣。

为了使其工作,我不得不利用RcWeakRefCell。有没有更简单的方法来实现这一目标?

use std::cell::RefCell;
use std::rc::Rc;

#[derive(Debug)]
struct Foo {
    i: i32,
}

impl Foo {
    fn get(&self) -> i32 {
        self.i
    }
    fn incr(&mut self) {
        self.i += 1
    }
}

fn retry<O, N>(mut operation: O, mut notify: N) -> i32
where
    O: FnMut() -> i32,
    N: FnMut() -> (),
{
    operation();
    notify();
    operation()
}

fn something(f: &mut Foo) {
    let f_rc = Rc::new(RefCell::new(f));
    let f_weak = Rc::downgrade(&f_rc);

    let operation = …
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
509
查看次数