相关疑难解决方法(0)

如何在结构的可变引用中为字段交换新值?

我有一个带字段的结构:

struct A {
    field: SomeType,
}
Run Code Online (Sandbox Code Playgroud)

给定a &mut A,如何移动值field并交换新值?

fn foo(a: &mut A) {
    let mut my_local_var = a.field;
    a.field = SomeType::new();

    // ...
    // do things with my_local_var
    // some operations may modify the NEW field's value as well.
}
Run Code Online (Sandbox Code Playgroud)

最终目标相当于一项get_and_set()行动.在这种情况下,我并不担心并发性.

rust

16
推荐指数
2
解决办法
2788
查看次数

如何替换Mutex中的值?

我有一个隐藏在后面的Git存储库Mutex:

pub struct GitRepo {
    contents: Mutex<GitContents>,
    workdir: PathBuf,
}
Run Code Online (Sandbox Code Playgroud)

我想查询它,但最多只查询一次:在查询之后,我想只使用我们第一次得到的结果.存储库具有git2::Repository结果的向量或向量.A RepositorySend但不是Sync.

enum GitContents {
    Before { repo: git2::Repository },
    After { statuses: Git },
}

struct Git {
    statuses: Vec<(PathBuf, git2::Status)>,
}
Run Code Online (Sandbox Code Playgroud)

GitContents枚举反映了一个事实,我们要么库进行查询,或查询它的结果,但不可能兼顾.

我试图让Rust通过将存储库转换为状态的函数来强制执行此属性,因为它生成状态向量时使用存储库:

fn repo_to_statuses(repo: git2::Repository, workdir: &Path) -> Git {
    // Assume this does something useful...
    Git { statuses: Vec::new() }
}
Run Code Online (Sandbox Code Playgroud)

但是,我不能让它Mutex玩得很好.到目前为止,我尝试编写一个GitRepo使用谓词查询a的函数P,替换Mutexif中尚未查询的值:

impl GitRepo { …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker

7
推荐指数
1
解决办法
1976
查看次数

是否可以在Vec上映射函数而不分配新的Vec?

我有以下内容:

enum SomeType {
    VariantA(String),
    VariantB(String, i32),
}

fn transform(x: SomeType) -> SomeType {
    // very complicated transformation, reusing parts of x in order to produce result:
    match x {
        SomeType::VariantA(s) => SomeType::VariantB(s, 0),
        SomeType::VariantB(s, i) => SomeType::VariantB(s, 2 * i),
    }
}

fn main() {
    let mut data = vec![
        SomeType::VariantA("hello".to_string()),
        SomeType::VariantA("bye".to_string()),
        SomeType::VariantB("asdf".to_string(), 34),
    ];
}
Run Code Online (Sandbox Code Playgroud)

我现在想调用transform每个元素data并将结果值存回data.现在,我当然可以做类似的事情,data.into_iter().map(transform).collect()但这将分配一个新的Vec.有没有办法在就地执行此操作,重用已分配的内存data?有一次Vec::map_in_place在Rust,但它已被删除了一段时间(我认为在1.4左右).

作为解决方法,我目前添加Dummy-variant SomeType,然后执行以下操作:

for x in …
Run Code Online (Sandbox Code Playgroud)

rust

6
推荐指数
2
解决办法
510
查看次数

如何移出作为选项的struct字段?

我想收集结构的更改并立即应用它们.基本大纲如下所示:

enum SomeEnum {
    Foo,
    Bar,
}

struct SomeStruct {
    attrib: SomeEnum,
    next_attrib: Option<SomeEnum>,
}

impl SomeStruct {
    pub fn apply_changes(&mut self) {
        if let Some(se) = self.next_attrib {
            self.attrib = se;
        }
        self.next_attrib = None;
    }
}
Run Code Online (Sandbox Code Playgroud)

这会产生以下编译器错误:

error[E0507]: cannot move out of borrowed content
  --> src/lib.rs:13:27
   |
13 |         if let Some(se) = self.next_attrib {
   |                     --    ^^^^ cannot move out of borrowed content
   |                     |
   |                     hint: to prevent move, use `ref se` or `ref mut se` …
Run Code Online (Sandbox Code Playgroud)

copy rust borrow-checker

6
推荐指数
1
解决办法
723
查看次数

暂时离开借来的内容

我想在可变借入中替换一个值; 将其中的一部分移动到新值中:

enum Foo<T> {
    Bar(T),
    Baz(T),
}

impl<T> Foo<T> {
    fn switch(&mut self) {
        *self = match self {
            &mut Foo::Bar(val) => Foo::Baz(val),
            &mut Foo::Baz(val) => Foo::Bar(val),
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用,并且可以理解的是,将值移出会self破坏它的完整性.但由于之后立即删除了该值,我(如果不是编译器)可以保证它的安全性.

有没有办法实现这个目标?我觉得这是一个不安全代码的工作,但我不确定这是如何工作的.

ownership rust borrow-checker

5
推荐指数
3
解决办法
1184
查看次数

标签 统计

rust ×5

borrow-checker ×3

copy ×1

ownership ×1