I'm wondering if anyone has a nicer pattern for how to work around the borrow checker in the following sort of pattern match (the nested ifs below). The enum I'm actually caring about is not an Option, otherwise I'd just change the outer if to check is_some, which would be "good enough". But is there a nicer way to check for a pattern, then do something with something inside the pattern, and then use a mutable reference to the thing that is matched?
fn needs_mut(_: &mut Option<String>) {
}
fn main() {
let mut v = Some(String::from("start"));
if let Some(_) = v {
if let Some(ref old) = v {
println!("old x is {}", old);
}
needs_mut(&mut v);
}
}
I believe the "real" solution to this is non-lexical lifetimes, so the compiler notice that the reference to "old" is no longer in use when I call needs_mut. But for now, does anyone have a prettier way to structure something like this?
Could you show an example of how would you write in a prettier way in another language, because I'm not too sure which part you don't like about this or what you are trying to achieve.
fn needs_mut(_: &mut Option<String>) {
}
fn main() {
let mut v = Some(String::from("start"));
if let Some(ref old) = v {
println!("old x is {}", old);
needs_mut(&mut v);
}
}
"Datasort refinement" could improve on this a bit. This is tracked in rust-lang/rfcs#754 and matching is covered in this proposal.
With datasort refinement, we wouldn't need a reference to old and a mutable reference to v in scope at the same time. Instead there could be a single reference of type &mut Option::Some through which we can access old but also pass it to needs_mut. Something like:
fn needs_mut(_: &mut Option<String>) {
}
fn main() {
let mut v = Some(String::from("start"));
if let ref mut s @ Some(_) = v {
println!("old x is {}", s.0);
needs_mut(s);
}
}
fn needs_mut(_: &mut Option<String>) {
}
fn main() {
let mut v = Some(String::from("start"));
if let Some(ref old) = v {
println!("old x is {}", old);
}
if v.is_some() {
needs_mut(&mut v);
}
}
Good point. I think I prefer the nesting, though, since then the reader of the code can skip over both at once (more easily) if they are thinking of the case where the pattern is not matched.
Another option, if the needs_mut is something you are designing is to make it Option<&mut String>. Than you can do
fn needs_mut(x: Option<&mut String>) {
let s = x.unwrap();
*s = s.to_lowercase();
}
fn main() {
let mut v = Some(String::from("Start"));
if let Some(ref mut old) = v {
println!("old x is {}", old);
needs_mut(Some(old));
}
println!("New x is {:?}", v);
}
But all that really depends on what exactly is the real use case for the function.
Alas, I'm not using Option at all, but rather an enum with several variants and fields. I'd rather not write the equivalent of unwrap for this enum, particularly as I only use this pattern a couple of times (so far) for this particular item.
fn main() {
let mut v = Some(String::from("start"));
if if let Some(ref old) = v {
println!("old x is {}", old);
true
} else {
false
} {
needs_mut(&mut v);
}
}
Oh I think I see what you're trying to achieve, writing out your enum would have been helpful here. Anyways, you could match your enum, move the value out, and on the needs_mut call rewrap said value with an enum again.
I don't know what I'm doing but this seems to work on stable:
fn needs_mut(_: &mut Option<String>) {
}
fn main() {
let v = Some(String::from("start"));
if let mut x @ Some(_) = v {
x.as_ref().map(|ref old| println!("old x is {}", old));
needs_mut(&mut x);
}
}
So you would need to provide as_ref and map for your enum.