I'm just beginning to learn a little Rust and am very intrigued by the concept of mutability of variables.
I'm trying to write something very similar to this C++ program.
#include <cstdio>
void do_something(int &var) {
var++;
}
int main() {
int a = 3;
int b = a;
printf("a is %d and b is %d\n", a, b);
do_something(b);
printf("a is %d and b is %d\n", a, b);
return 0;
}
I expect to see:
a is 3 and b is 3
a is 3 and b is 4
The idea is that the pass-by-reference renders b
mutable, but a
is not mutable.
Here's how I would assume to write this program in Rust:
fn main() {
let a: i32 = 3;
let b: &mut i32 = &a;
println!("a is {} and b is {}", a, b);
do_something(b);
println!("a is {} and b is {}", a, b);
}
fn do_something(var: &mut i32) {
(*var)+=1;
}
However, I get errors due to mismatched mutability.
error: mismatched types:
expected
&mut i32
, found&i32
(values differ in mutability) [E0308]
Are there ways to secure this pass-by-reference style in Rust without ::New
? My guess is I could use .clone()
, but I'm not positive.