As already mentioned in the comments of Aurora0001, we should take a look at the signature of the cmp() function :
fn cmp(&self, other: &Self) -> Ordering
We see that both values ββare taken by reference, so it should not be a surprise that you should pass &b.1 method instead of b.1 .
Moreover, I do not need to refer to a.1 .
This is a more interesting question ;-)
A simple solution is that of an operator . (dot) performs automatic dereferencing as well as auto-plotting. Look at this in action:
struct Foo; impl Foo { fn takes_value(self) {} fn takes_ref(&self) {} fn takes_mut_ref(&mut self) {} } fn main() { let mut a = Foo;
So the operator . helps the programmer and always passes the correct type of self parameter.
Note : the sorting you do very often. There is an even more suitable method for this task: [T]::sort_by_key() . It will look like this:
// note: type annotations not required let mut tuple_list2 = vec![(1, 5), (0, 17), (8, 2)]; tuple_list2.sort_by_key(|k| k.1);
source share