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?