These are chat archives for rust-lang/rust

6th
Jun 2017
Michael Thomas
@Michaelt293
Jun 06 2017 09:20

Slightly stuck on a problem -

fn selection_sort(a: &mut Vec<i32>) {
    for i in 0..a.len() - 1 {
        let min_value = a[i..a.len()].iter().min();
        a[i] = min_value.unwrap();
    }
}

 --> src/main.rs:41:16
   |
41 |         a[i] = min_value.unwrap();
   |                ^^^^^^^^^^^^^^^^^^ expected i32, found &i32
   |
   = note: expected type `i32`
              found type `&i32`

How can I pass a reference when it's expecting a i32?

Denis Lisov
@tanriol
Jun 06 2017 09:22
IIRC, iter iterates over references to elements... *min_value.unwrap(), maybe?
Michael Thomas
@Michaelt293
Jun 06 2017 09:24
thanks, then I get
error[E0502]: cannot borrow `*a` as mutable because it is also borrowed as immutable
  --> src/main.rs:45:9
   |
44 |         let min_value = a[i..a.len()].iter().min();
   |                         - immutable borrow occurs here
45 |         a[i] = *min_value.unwrap();
   |         ^ mutable borrow occurs here
46 |     }
   |     - immutable borrow ends here
arhhhh got it to compile and now realise I made a terrible mistake, ha. Thanks again
Denis Lisov
@tanriol
Jun 06 2017 09:27
The mistake of not preserving the previous value? :-)
Michael Thomas
@Michaelt293
Jun 06 2017 09:31
selection sorted vector: [26, 26, 26, 26, 26, 26] .... a vec containing only the minimum value....doh
Denis Lisov
@tanriol
Jun 06 2017 09:43
I'd suggest looking into slice::split_first_mut and std::mem::swap
gdfreeman
@gdfreeman
Jun 06 2017 20:37
What is tick or ' operator called in rust? I'm trying to find it in the documentation.
Alexander Ronald Altman
@pthariensflame
Jun 06 2017 20:39
I think most people pronounce it “tick” or “prime”, or just leave it silent.
I doesn't have an official name, as far as I know.
Sean Leffler
@sdleffler
Jun 06 2017 20:40
I call it "prime".
Pascal Hertleif
@killercup
Jun 06 2017 20:40
@gdfreeman not sure what it's called, but have you seen https://doc.rust-lang.org/book/syntax-index.html?
gdfreeman
@gdfreeman
Jun 06 2017 20:45
@killercup Oh thank you! It's called a lifetime I believe. It gives an example T: 'a where type T must outlive lifetime 'a.
Pascal Hertleif
@killercup
Jun 06 2017 20:46
@gdfreeman ah, yes. in 'a it means "a lifetime (think variable scope) called a", but the single quote is also used like 'x' to mean "the character 'x'"
Alexander Ronald Altman
@pthariensflame
Jun 06 2017 20:49
It's also used for code labels (with the same syntax as lifetimes).
Ashley Mannix
@KodrAus
Jun 06 2017 23:41
Hey :) Is there a standard type for wrapping a sequence of non-contiguous Reads that itself is Read? I've put together a quick one myself that looks a bit like ByteSequence(VecDeque<Cursor<Vec<u8>>>) which implements Read but is a bit slow. I've got some thoughts for greasing it up, but thought I'd see if there's something out there already