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


am missing something, or mutable non-reference arguments not supported in rust?

to give example, playing rust , tried implement euclid's algorithm generic numeric types, , ideally wanted pass arguments value , have them mutable, adding keyword mut argument type rejected compiler. have declare mutable copy of argument function prologue. idiomatic/efficent?

use std::ops::rem;  extern crate num; use self::num::zero;  pub fn gcd<t: copy + 0 + 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 } 

it's possible argument mutable:

pub fn gcd<t>(mut a: t, mut b: t) -> t     t: copy + 0 + partialord + rem<output=t> {    while b > t::zero() {       let t = b;       b = % b;        = t;    }     } 

is [declaring mutable copy of argument] idiomatic/efficient?

it should fine efficiency perspective. optimizer see same , not extraneous copying.

as idiomatic, i'm not sure. started not putting mut in function argument list felt oversharing details implementation. nowadays, go ahead , put in there.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -