Mutate a value stored in an enum in a Vec - rust

The following code fails to compile with the error below:
enum Test {
C(i32),
}
fn main() {
let mut v = Vec::new();
v.push(Test::C(0));
if let Some(Test::C(ref mut c)) = v.last_mut() {
*c = *c + 1;
}
}
error[E0308]: mismatched types
--> src/main.rs:10:17
|
10 | if let Some(Test::C(ref mut c)) = v.last_mut() {
| ^^^^^^^^^^^^^^^^^^ expected &mut Test, found enum `Test`
|
= note: expected type `&mut Test`
found type `Test`
last_mut() returns a mutable reference, and I'm taking the i32 as a mutable reference. I've tried making the mutability even more clear as follows, but I get the same compiler error.
if let Some(ref mut e) = v.last_mut() {
if let Test::C(ref mut c) = e {
*c = *c + 1;
}
}
Why doesn't this work?

You just need to match exactly what the error message says. It expects a &mut Test, so you should match on that:
if let Some(&mut Test::C(ref mut c)) = v.last_mut() {
// ^^^^^^
*c = *c + 1;
}
Here it is running in the playground.
As of Rust 1.26, your original code works as-is and the explicit ref and ref mut keywords are no longer required:
if let Some(Test::C(c)) = v.last_mut() {
*c = *c + 1;
}

Related

Mutable vs. immutable borrows in closure?

I can't figure out how to get the following to work. I think I need the closure to borrow by &mut Vec, but I don't know how to express that. This is distilled from a larger function, but shows the same error.
fn main() {
let mut v = vec![0; 10];
let next = |i| (i + 1) % v.len();
v[next(1usize)] = 1;
v.push(13);
v[next(2usize)] = 1;
}
Error:
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
--> a.rs:9:5
|
5 | let next = |i| {
| --- immutable borrow occurs here
6 | (i + 1) % v.len()
| - first borrow occurs due to use of `v` in closure
...
9 | v[next(1usize)] = 1;
| ^ ---- immutable borrow later used here
| |
| mutable borrow occurs here
error: aborting due to previous error
If you really want to do it with a closure, you will have to pass the vector by parameter:
let next = |v: &Vec<_>, i| (i + 1) % v.len();
This makes the closure borrow per-call, rather than capture for the scope. You still need to separate the borrows, though:
let j = next(&v, 1usize);
v[j] = 1;
To make your life easier, you can put everything inside the closure instead:
let next = |v: &mut Vec<_>, i, x| {
let j = (i + 1) % v.len();
v[j] = x;
};
Which allows you to simply do:
next(&mut v, 1usize, 1);
next(&mut v, 2usize, 2);
// etc.
This pattern is useful for cases where you are writing a closure just for avoiding local code repetition (which I suspect is why you are asking given the comments).
Since the closure only needs the length of the Vec. Then instead, you can just get that prior to the closure. Then you avoid the whole borrowing issue, as the closure doesn't need to borrow v anymore.
fn main() {
let mut v = vec![0; 10];
let len = v.len();
let next = |i| (i + 1) % len;
v[next(1usize)] = 1;
}
Assuming your closure is not dependent on other things, then instead of a closure, you could define a trait with a method that does that.
For simplicity let's call the trait VecExt and the method set.
trait VecExt<T> {
fn set(&mut self, index: usize, value: T);
}
impl<T> VecExt<T> for Vec<T> {
fn set(&mut self, index: usize, value: T) {
let len = self.len();
self[(index + 1) % len] = value;
}
}
fn main() {
let mut v = vec![0; 10];
v.set(1, 1);
v.push(13);
v.set(2, 1);
}
A closure probably isn't the right tool for the job. The compiler is unhappy because your closure has taken a reference against your Vec, but then while that closure reference is still outstanding you're trying to mutate the Vec. Under Rust's borrow rules, that's not allowed.
The most straightforward approach would be storing your data inside a struct, and making next a member function. That way, there's no closure taking references; it can just check the length only when needed.
struct WrapVec<T>(Vec<T>);
impl<T> WrapVec<T> {
fn wrap_next(&mut self, index: usize) -> &mut T {
let index = (index + 1) % self.0.len();
&mut self.0[index]
}
}
fn main() {
let mut v = WrapVec(vec![0; 10]);
*v.wrap_next(1) = 1;
v.0.push(13);
*v.wrap_next(2) = 1;
}
If you want to be able to apply this function to any Vec, then you may find it useful to define a new trait. For example:
trait WrapNext<T> {
fn wrap_next(&mut self, index: usize) -> &mut T;
}
impl<T> WrapNext<T> for Vec<T> {
fn wrap_next(&mut self, index: usize) -> &mut T {
let index = (index + 1) % self.len();
&mut self[index]
}
}
fn main() {
let mut v = vec![0; 10];
*v.wrap_next(1) = 1;
v.push(13);
*v.wrap_next(2) = 1;
}
In addition to the other answers here, you can use a macro to scope v so you don't have to pass it in every call:
fn main() {
let mut v = vec![0; 10];
macro_rules! next {
// rule to get an index
($i: expr) => {
($i + 1) % v.len()
};
// rule to mutate the vector
($i: expr => $v: expr) => {{
let ind = next!($i);
v[ind] = $v;
}};
};
// get an index
let ind = next!(1usize);
// mutate the vector
v[ind] = 1;
v.push(13);
// or, with the mutation syntax
next!(2usize => 3);
println!("{:?}", v);
}

Create closure returning iterator on string

I want to write a closure that takes an object and returns an iterator from it. The idea is to store the closure in a structure and apply as needed:
fn main() {
let iter_wrap = |x: &String| Box::new(x.chars());
let test = String::from("test");
for x in iter_wrap(&test) {
println!("{}", x);
}
}
This causes the error:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/main.rs:2:45
|
2 | let iter_wrap = |x: &String| Box::new(x.chars());
| ^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 2:21...
--> src/main.rs:2:21
|
2 | let iter_wrap = |x: &String| Box::new(x.chars());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:2:43
|
2 | let iter_wrap = |x: &String| Box::new(x.chars());
| ^
note: but, the lifetime must be valid for the call at 5:14...
--> src/main.rs:5:14
|
5 | for x in iter_wrap(&test) {
| ^^^^^^^^^^^^^^^^
note: ...so that argument is valid for the call
--> src/main.rs:5:14
|
5 | for x in iter_wrap(&test) {
| ^^^^^^^^^^^^^^^^
I tried to change String to Vec and remove boxing, but the result is the same.
How can I make it compile?
Closures with borrows in parameter or return types have some known bugs as shown in this issue report and the others it links to: https://github.com/rust-lang/rust/issues/58052
There are a few ways to work around the issue.
Using fully qualified syntax
fn main() {
let iter_wrap = |x| Box::new(str::chars(x));
let test = String::from("test");
for x in iter_wrap(&test) {
println!("{}", x);
}
}
Using a type annotation in the closure body
fn main() {
let iter_wrap = |x| {let x: &String = x; Box::new(x.chars()) };
let test = String::from("test");
for x in iter_wrap(&test) {
println!("{}", x);
}
}
I'm not entirely sure what you try to achieve there, but basically just looking at your provided example, you don't need a closure, but a function:
use std::str::Chars;
fn main() {
fn iter_wrap(s: &str) -> Chars {
s.chars()
}
let test = "test".to_string();
for c in iter_wrap(&test) {
println!("{}", c);
}
}
Or you could have a closure, that is enclosing the outside world, in this case, your string:
fn main() {
let test = "test".to_string();
let iter_wrap = || test.chars();
for c in iter_wrap() {
println!("{}", c);
}
}
You need to define and use a lifetime that is shorter than the lifetime of the function itself. I tried this and it worked:
fn foo<'a, 'b: 'a>() {
let test = String::from("test");
let iw = |x: &'b String| {
x.chars()
};
for x in iw(&test) {
println!("{}", x);
}
}
fn main() {
foo()
}
Just using lifetime 'a is not sufficient, you need a lifetime that is shorter than that, so 'b: 'a.
Now what I cannot explain is that, with both 'a and 'b defined, using &'a String in the closure definition works as well ...

Is it possible to create a mutable value of a mutable reference in a pattern?

When pattern-matching, you can specify that you'd like to get a mutable reference to the contained value by using ref mut:
let mut score = Some(42);
if let Some(ref mut s) = score {
&mut s;
}
However, the inner value is not mutable:
error[E0596]: cannot borrow immutable local variable `s` as mutable
--> src/main.rs:4:14
|
4 | &mut s;
| ^
| |
| cannot reborrow mutably
| try removing `&mut` here
I tried to add in another mut, but that was not valid:
if let Some(mut ref mut s) = score {
&mut s;
}
error: the order of `mut` and `ref` is incorrect
--> src/main.rs:3:17
|
3 | if let Some(mut ref mut s) = score {
| ^^^^^^^ help: try switching the order: `ref mut`
error: expected identifier, found keyword `mut`
--> src/main.rs:3:25
|
3 | if let Some(mut ref mut s) = score {
| ^^^ expected identifier, found keyword
error: expected one of `)`, `,`, or `#`, found `s`
--> src/main.rs:3:29
|
3 | if let Some(mut ref mut s) = score {
| ^ expected one of `)`, `,`, or `#` here
Not a direct answer, but possible workarounds
Create an intermediate variable
if let Some(ref mut s) = score {
let mut s = s;
&mut s;
}
#[derive(Debug)]
struct X;
enum Foo<T> {
Bar(T),
_Baz,
}
fn main() {
let mut score = Foo::Bar(X);
if let Foo::Bar(ref mut s) = score {
//let x = s;
//println!("{:?}", **x); ! not possible
let x = &mut &mut *s; // &mut &mut X
println!("{:?}", **x);
}
}
For Option specifically
if let Some(ref mut s) = score.as_mut() {
s; //:&mut &mut i32
}
if let Some(mut s) = score.as_mut() {
&mut s;
}
Below code may give an idea for the possible solution to the problem. It's just a sample & testable code to provide a tiny example that aimed at the issue. Of course it may not cover the whole intents and purposes.
fn main() {
let mut score = Some(42i32);
let res = if let Some(41) = score {
println!("41 is matched");
1i32
} else if let Some(ref mut s) = score { //&mut score {
//let mut s2 = s;
//println!("s: {:#?}", s);
test(&mut &mut *s); // This part may be like this for borrowing
//println!("s: {:#?}", s);
1i32
} else {
0i32
};
//println!("Result: {:#?}", score);
assert_eq!(res, 1i32);
}
fn test(ref mut s: &mut &mut i32) -> i32 {
//let mut s2 = s;
return test2(&mut *s);
}
fn test2(n: &mut i32) -> i32 {
*n += 1;
//println!("Value: {}", *(*n));
return *n;
}
Live Version: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7c3e7e1ee712a31f74b201149365035f
Gist Link: https://gist.github.com/7c3e7e1ee712a31f74b201149365035f

How do I modify the content of a `RefCell<Option<T>>`?

I have an Option<T> that is shared by several structures and that must be mutable. I'm using a RefCell since, as I understand, it is the tool for that job. How do I access (and alter) the content of that Option<T> ?
I tried the following:
use std::cell::RefCell;
#[derive(Debug)]
struct S {
val: i32
}
fn main() {
let rc: RefCell<Option<S>> = RefCell::new(Some(S{val: 0}));
if let Some(ref mut s2) = rc.borrow_mut() {
s2.val += 1;
}
println!("{:?}", rc);
}
But the compiler won't let me do it:
error[E0308]: mismatched types
--> <anon>:10:12
|
10 | if let Some(ref mut s2) = rc.borrow_mut() {
| ^^^^^^^^^^^^^^^^ expected struct `std::cell::RefMut`, found enum `std::option::Option`
|
= note: expected type `std::cell::RefMut<'_, std::option::Option<S>, >`
found type `std::option::Option<_>`
When you borrow_mut the RefCell, you get a RefMut, as the compiler says. To get the value inside it, just use the operator deref_mut:
use std::cell::RefCell;
#[derive(Debug)]
struct S {
val: i32
}
fn main() {
let rc: RefCell<Option<S>> = RefCell::new(Some(S{val: 0}));
if let Some(ref mut s2) = *rc.borrow_mut() { // deref_mut
s2.val += 1;
}
println!("{:?}", rc);
}

When can I omit dereferencing with the asterisk operator?

I have a hard time understanding when to use the asterisk operator for dereferencing and when can I omit it.
fn main() { a(); b(); c(); d(); }
fn a() {
let v = 1;
let x = &v;
println!("a {}", *x);
println!("a {}", 1 + *x);
}
fn b() {
let v = 1;
let x = &v;
println!("b {}", x);
println!("b {}", 1 + x);
}
fn c() {
let mut v = 1;
let mut x = &mut v;
println!("c {}", *x);
println!("c {}", 1 + *x);
}
fn d() {
let mut v = 1;
let mut x = &mut v;
println!("d {}", x);
println!("d {}", 1 + x); // error
}
The above code sample almost compiles except the last statement where I add one to the the mutable reference x. There I get this error:
the trait bound `_: std::ops::Add<&mut _>` is not satisfied [E0277]
Anywhere else both asterisk and non-asterisk versions are valid and give expected results.
You can only add types for which the operator is defined by implementing the Add trait. In both examples where you do 1 + *x, the type of *x is i32 and indeed i32 + i32 is defined. For convenience there is also an implementation of i32 + &i32 (and &i32 + i32 and &i32 + &i32) which makes b work, but this is just for the specific case of one immutable reference, it doesn't extend to, say &&i32 or &mut i32. That's why d does not work.

Resources