These are chat archives for rust-lang/rust

7th
Jun 2017
Michael Thomas
@Michaelt293
Jun 07 2017 07:38

Still working on the same algorithm. Here's where I'm at now -

fn selection_sort(a: &mut Vec<i32>) {
    let vec_length = a.len();
    for i in 0..vec_length {
        let min_value = a[i..vec_length].iter().min().unwrap();
        if let Some(min_index) = a.iter().position(|e| e == min_value) {
            a.swap(i, min_index);
        }
    }
}

Which give me the following error -

error[E0502]: cannot borrow `*a` as mutable because it is also borrowed as immutable
  --> src/main.rs:44:13
   |
42 |         let min_value = a[i..vec_length].iter().min().unwrap();
   |                         - immutable borrow occurs here
43 |         if let Some(index) = a.iter().position(|e| e == min_value) {
44 |             a.swap(i, index);
   |             ^ mutable borrow occurs here
45 |         }
46 |     }
   |     - immutable borrow ends here

The error message is very useful and I understand it. The problem is I don't know how to get around this problem.....any ideas?

mayank srivastava
@mighty_1989_twitter
Jun 07 2017 07:42
HI
i am very new to rust
can anyone tell me what will be the good starting point for a newbie
thank you
Michael Thomas
@Michaelt293
Jun 07 2017 07:44
Reading the Rust book? It's free and is quite enjoyable to read.
Jonas Platte
@jplatte
Jun 07 2017 07:44
@mighty_1989_twitter I'd recommend the second edition of the rust book: https://doc.rust-lang.org/nightly/book/second-edition/
Ryan
@rnleach
Jun 07 2017 07:45
http://rustbyexample.com/ goes along nicely with the book too.
mayank srivastava
@mighty_1989_twitter
Jun 07 2017 07:46
Thank you
Jonas Platte
@jplatte
Jun 07 2017 07:46
(the second edition book isn't fully finished yet, but most of earlier chapters are done, and the later ones have also been mostly written, and just need to go through a few revisions)
Ashley Mannix
@KodrAus
Jun 07 2017 07:46
@Michaelt293 The issue you're hitting is because iter() iterates over &T, rather than owned Ts. So if you tweak your code a bit like this then it will compile:
fn selection_sort(a: &mut Vec<i32>) {
    let vec_length = a.len();
    for i in 0..vec_length {
        let min_value = a[i..vec_length].iter().min().unwrap().to_owned();

        if let Some(min_index) = a.iter().position(|e| *e == min_value) {
            a.swap(i, min_index);
        }
    }
}
In your sample above, min_value is actually a &i32, which is borrowed from a
Fra ns
@snarf95_twitter
Jun 07 2017 07:51
@Michaelt293 also note that this function can be optimized way more... I would probably use enumerate() to find the min index too.
Denis Lisov
@tanriol
Jun 07 2017 07:53
@snarf95_twitter I'd suggest going not down to indices, but up to slices...
Michael Thomas
@Michaelt293
Jun 07 2017 09:53
Thanks @KodrAus @snarf95_twitter
Michal 'vorner' Vaner
@vorner
Jun 07 2017 11:36
Hello. I noticed that the syntax for lifetimes and for naming cycles (so break can exit the correct one, not just the internmost one) looks very much the same. Is there a reason to that (other than both refering to some block of code)?
Daan Rijks
@ijks
Jun 07 2017 11:55
@vorner I don't think there's any significance to it, but you can see lifetimes as an identifier for a 'scope' in a way. Then naming a loop with a 'label is essentially naming the scope of that loop. But I don't think you can use a loop label where a lifetime is expected.
Michal 'vorner' Vaner
@vorner
Jun 07 2017 11:58
I discovered that much myself already. But I was interested if there was some pre-1.0 history or something.
Denis Lisov
@tanriol
Jun 07 2017 12:01
The syntax was changed to that rust-lang/rust@670ab8a, but I can't find any reasoning for this change.
Daan Rijks
@ijks
Jun 07 2017 12:01
Hm, dunno. Maybe ask on Reddit or IRC? There should be some long-time/knowledgeable contributors there.
Michal 'vorner' Vaner
@vorner
Jun 07 2017 12:02
I don't think it's that important to spam on multiple places O:-).