Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the idiomatic way to pass by mutable value?

Tags:

rust

Am I missing something, or are mutable non-reference arguments not supported in Rust?

To give an example, I was playing with Rust and tried to implement Euclid's algorithm generic for all numeric types, and ideally I just wanted to pass arguments by value and have them mutable, but adding keyword mut to the argument type is rejected by compiler. So I have to declare a mutable copy of the argument as the function prologue. Is this idiomatic/efficent?

use std::ops::Rem;

extern crate num;
use self::num::Zero;

pub fn gcd<T: Copy + Zero + PartialOrd + Rem<Output=T>>(a : T, b : T) -> T
{
   let mut aa = a;
   let mut bb = b;

   while bb > T::zero() {
      let t = bb;
      bb = aa % bb; 
      aa = t;
   }

   aa
}
like image 432
Max Galkin Avatar asked Jul 13 '15 16:07

Max Galkin


1 Answers

It's certainly possible to say that an argument will be mutable:

use num::Zero; // 0.4.0
use std::ops::Rem;

pub fn gcd<T>(mut a: T, mut b: T) -> T
where
    T: Copy + Zero + PartialOrd + Rem<Output = T>,
{
    while b > T::zero() {
        let t = b;
        b = a % b;
        a = t;
    }

    a
}

Is [declaring a mutable copy of the argument] idiomatic/efficient?

It should be fine from an efficiency perspective. The optimizer will see that they are the same and not do any extraneous copying.

As for idiomatic, I'm not so sure. I originally started by not putting mut in my function argument list as I felt that it was oversharing details about the implementation. Nowadays, I go ahead and put it in there.

like image 127
Shepmaster Avatar answered Nov 15 '22 09:11

Shepmaster