Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using Rust does passing a mutable struct to a function result in immutable fields?

Tags:

rust

rust-0.8

I'm learning Rust using 0.8 on Win8-64. I have a test program I'm working on where a function that handles input of parameters returned a struct containing those parameters. That worked OK. I then altered the program to pass the &struct to the function, and I now get a compiler error that I'm attempting to assign to an immutable field.

How should I be passing a pointer/reference to the struct to prevent this error?

The code that results in the error (I've tried a few variations) :

let mut ocParams : cParams = cParams::new();     //!!!!!! This is the struct passed

fInputParams(&ocParams);               // !!!!!!! this is passing the struct

if !ocParams.tContinue {
    return;
}

.......

struct cParams {
  iInsertMax : i64,
  iUpdateMax : i64,
  iDeleteMax : i64,
  iInstanceMax : i64,
  tFirstInstance : bool,
  tCreateTables : bool,
  tContinue : bool
}

impl cParams {
  fn new() -> cParams {
     cParams {iInsertMax : -1, iUpdateMax : -1, iDeleteMax : -1, iInstanceMax : -1,
              tFirstInstance : false, tCreateTables : false, tContinue : false}
  }   
}

.....

fn fInputParams(mut ocParams : &cParams) {

    ocParams.tContinue = (sInput == ~"y");    // !!!!!! this is one of the error lines

All of the assignments to the struct fields in the function result in an error at compile-time. An example of the errors that result from compile :

testli007.rs:240:2: 240:20 error: cannot assign to immutable field
testli007.rs:240   ocParams.tContinue = (sInput == ~"y");   
like image 664
Brian Oh Avatar asked Oct 29 '13 03:10

Brian Oh


People also ask

Are struct fields mutable Rust?

However, in Rust, mutability is an all-or-nothing attribute: either a variable is declared as mutable and all of its fields are also mutable (if it is a struct ), or it's declared immutable and so are all of its fields.

What is a mutable reference Rust?

Back to Rust. A mutable reference is a borrow to any type mut T , allowing mutation of T through that reference. The below code illustrates the example of a mutable variable and then mutating its value through a mutable reference ref_i .

How do you pass a struct to a function in Rust?

We can pass a struct to a function by specifying the struct name as the type in the parameter list. We can return a struct from a function by specifying the struct name as the return type. We can define functions that are specific to a struct, called methods, that can only be used by instances of that struct.

What is mutability in Rust?

Mutability is a property of either a borrow ( &mut ) or a binding ( let mut ). This means that, for example, you cannot have a struct with some fields mutable and some immutable: struct Point { x: i32, mut y: i32, // Nope. }


1 Answers

In the declaration of your function:

fn fInputParams(mut ocParams : &cParams) {

ocParams is a mutable variable containing a borrowed pointer to an immutable struct. What you want is for that struct to be mutable, not the variable. Therefore, the signature of the function should be:

fn fInputParams(ocParams : &mut cParams) {

Then you have to change the call itself to fInputParams:

fInputParams(&mut ocParams);  // pass a pointer to mutable struct.
like image 140
Cyrille Ka Avatar answered Sep 27 '22 17:09

Cyrille Ka