使用 mpd 的 Query::and 时,不能一次多次借用 `xxx` 作为可变变量 [英] cannot borrow `xxx` as mutable more than once at a time when using mpd's Query::and

查看:49
本文介绍了使用 mpd 的 Query::and 时,不能一次多次借用 `xxx` 作为可变变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下功能,该功能需要 3 个可选输入并从 rust-mpd 板条箱中的 mpd 客户端获取搜索结果.

I am trying to realize the following function, which takes 3 optional input and get search result from mpd client from the rust-mpd crate.

fn load(
    &mut self,
    track: &Option<String>,
    artist: &Option<String>,
    album: &Option<String>
) -> Result<(), &'static str>{
    println!("mpd, load method is invoked with:");
    let mut query = Query::new();

    if let Some(s) = track {
        println!("  track: {}", &s);
        query.and(Term::Tag(Cow::Borrowed("track")), s);
    }

    if let Some(s) = artist {
        println!("  artist: {}", &s);
        query.and(Term::Tag(Cow::Borrowed("artist")), s);
    }

    if let Some(s) = album {
        println!("  album: {}", &s);
        query.and(Term::Tag(Cow::Borrowed("album")), s);
    }

    //let musics = self.client.search(&query, Window(None));
    let musics = self.client.search(&query, (1,2));
    println!("Search result: {:?}", musics);

    Ok(())
}

但是,当我尝试编译时,它告诉我:

However, when I try to compile, it tells me:

error[E0499]: cannot borrow `query` as mutable more than once at a time
  --> src/player/mpd.rs:62:13
   |
57 |             query.and(Term::Tag(Cow::Borrowed("track")), s);
   |             ----- first mutable borrow occurs here
...
62 |             query.and(Term::Tag(Cow::Borrowed("artist")), s);
   |             ^^^^^
   |             |
   |             second mutable borrow occurs here
   |             first borrow later used here

error[E0499]: cannot borrow `query` as mutable more than once at a time
  --> src/player/mpd.rs:67:13
   |
62 |             query.and(Term::Tag(Cow::Borrowed("artist")), s);
   |             ----- first mutable borrow occurs here
...
67 |             query.and(Term::Tag(Cow::Borrowed("album")), s);
   |             ^^^^^
   |             |
   |             second mutable borrow occurs here
   |             first borrow later used here

error[E0502]: cannot borrow `query` as immutable because it is also borrowed as mutable
  --> src/player/mpd.rs:71:41
   |
67 |             query.and(Term::Tag(Cow::Borrowed("album")), s);
   |             ----- mutable borrow occurs here
...
71 |         let musics = self.client.search(&query, (1,2));
   |                                         ^^^^^^
   |                                         |
   |                                         immutable borrow occurs here
   |                                         mutable borrow later used here

如果我的方法是错误的,我应该如何实现相同的功能?

If my way is wrong, how should I achieve the same functionality?

推荐答案

乍一看,可变借用应该以 if let 块结束,这将允许后续借用.然而,rust-mpd 中存在一个错误,导致借用被延长到 query 的整个生命周期:Query::and 方法声明为:

On first sight, the mutable borrow should end with the if let block, which would allow the subsequent borrows. However there is a bug in rust-mpd that causes the borrow to be extended for the full lifetime of query: the Query::and method is declared as:

fn and<'b: 'a, V: 'b + Into<Cow<'b, str>>>(
    &'a mut self,         // <- Note the 'a lifetime here
    term: Term<'b>,
    value: V)
    -> &'a mut Query<'a>  // <- Note the **two** 'a lifetimes here

使用相同的生命周期'a 用于查询的引用嵌入的生命周期.该方法应该像这样声明:

using the same lifetime 'a for both the references to the query and the embedded lifetime. The method should be declared like this:

fn and<'b: 'a, V: 'b + Into<Cow<'b, str>>>(
    &mut self,
    term: Term<'b>,
    value: V)
    -> &mut Query<'a>

您应该向 rust-mpd 报告错误.

You should report a bug to rust-mpd.

这篇关于使用 mpd 的 Query::and 时,不能一次多次借用 `xxx` 作为可变变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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