Going through some exercises on basic Rust syntax and ownership.

Links:

Rust 101 is a series of videos explaining how to write programs in Rust. The course materials for this series are developed by tweede golf. You can find more information at https://github.com/tweedegolf/101-rs and you can sponsor the work at https://github.com/sponsors/tweedegolf . They are released under the Creative Commons Attribution Share Alike 4.0 International license.

This series of videos is copyright 2023 Andy Balaam and the tweede golf contributors and is released under the Creative Commons Attribution Share Alike 4.0 International license.

  • maegul (he/they)
    link
    fedilink
    English
    arrow-up
    1
    ·
    9 months ago

    Hey again …

    THinking about 2-move-semantics/03.rs (around 35 mins). I didn’t think of your solution (just adding mut to the argument of append_to_string! I didn’t know you could do that! Certainly quick and elegant!

    I couldn’t help but wonder though that if you wanted the argument you’d have to change a few more things about the place, essentially making the variable mutable from its initialisation and adding reference types down the line … certainly a bit more work. Unless I’m missing something?

    Any best-practice style thoughts on simply making a moved variable mutable in a new scope rather than borrowing it?

    • Andy Balaam@diode.zoneOP
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      9 months ago

      @maegul@lemmy.ml Yeah if you wanted to modify the original variable you would need to make it mutable, and use &mut references to it all the way down. By changing the function parameter to be mutable, we’re not modifying anything outside of this function: we’re just allowing ourselves to re-use the parameter to hold different values while the function is running. I don’t particularly like doing that - I’d prefer to make a new local variable if I want to change things within the function, but I think the exercise might have said I wasn’t allowed to make new variables :-)

      • maegul (he/they)
        link
        fedilink
        English
        arrow-up
        1
        ·
        9 months ago

        Cheers Andy!

        Also sorry for the gobbled English in my question! Thanks for making sense of it!

  • maegul (he/they)
    link
    fedilink
    English
    arrow-up
    1
    ·
    9 months ago

    Heya Andy!

    Thoughts on using or not using .sort() in exercise 1-basic-syntax/03.rs rather than rolling your own code for determining minimum and maximum values??

    For me, I just (lazily) landed on:

    fn main() {
        let mut input = [23, 82, 16, 45, 21, 94, 12, 34];
    
        // TODO
        input.sort();
        let (smallest, largest) = (input[0], input[input.len()-1]);
    
        println!("{largest} is largest and {smallest} is smallest");
    }
    
    • Andy Balaam@diode.zoneOP
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      9 months ago

      @maegul@lemmy.ml using sort looks like a nice solution. For very large lists it will be doing more work than is strictly necessary, but for most use cases that would be great, and nice and simple.

      • maegul (he/they)
        link
        fedilink
        English
        arrow-up
        1
        ·
        9 months ago

        Thanks! Yea I’m clearly taking the easy route there with sort.

        Though it did make wonder why there’s no max or min methods even though there’s a sort … question for another time perhaps 🤔