Pattern restriction for &mut in Traits

Why I'm not allowed to:

pub trait List<T>
{
	fn get(&self,index:i32,&mut elem:T)->Self;
    ...
}

How to circumvent this kind of situation.

You've probably meant elem: &mut T (take a refernce to T, not take T by value and dereference it). The restriction exists partly to catch that mistake.

Patterns don't affect calling convention. They are totally meaningless in traits and do nothing there. That's because patterns work inside of function after it receives its arguments.

For example:

fn get(&val: &i32, (a, b): (u8, u8))

is identical to:

fn get(val_ref: &i32, ab: (u8, u8)) {
   let val = *val_ref;
   let (a, b) = ab;
}

and because traits don't have function bodies, the pattern part is never executed, and doesn't do anything.

1 Like

Thanks,
that was I'm looking for.

Is &mut x:i8 generally x:&mut i8

No, they are not similar at all. The left side of : has the opposite meaning of the right side. & on the right is a reference, & on the left is a dereference.

1 Like

Thanks