• @meliaesc
    link
    1410 months ago

    This was my first time actually seeing a Rust example, and I hate it.

    • xor
      link
      fedilink
      English
      1510 months ago

      You’ll be happy to hear I’ve updated the example to be not bad

      • @[email protected]
        link
        fedilink
        5
        edit-2
        10 months ago

        I wanted to ask why it’s bad, what did you change?

        Btw. the example function get_default is badly chosen, because unwrap_or_default exists.

        • xor
          link
          fedilink
          English
          1110 months ago

          The original example was doing the unwrap_within an iterator doing some string parsing, so there was a lot of unrelated boilerplate around the actual unwrapping that made it really unclear, as well as usual unwrap_or_else to produce a constant value

          Ehhh, I was more using get_default as a placeholder for some function, as opposed to representing Default::default for the inner type specifically. I think it should be alright since only people familiar with rust would know about the default trait anyway. I did consider adding an unwrap_or_default example, but thought it was getting a bit off topic at that point.

          • @[email protected]
            link
            fedilink
            1
            edit-2
            10 months ago
             .map(|n| n.parse().unwrap_or_else(|_| std::i64::MIN))
            

            *shudder*

            unwrap_or_else and passing a closure that ignores its argument is a definitive smell. Probably gets caught by clippy.

    • @[email protected]
      link
      fedilink
      1210 months ago

      Other languages: if a is null return b.

      Rust: here is an array of strings, we are going to parse the array to numbers. If that conversion fails we handle the exception and return the minimum integer value. We then save the result in a new vector. We also print it.

      I like rust, but I hate the example too. It’s needlessly complex. Should have just been a.unwrap_or(b).

      • @mea_rah
        link
        3
        edit-2
        10 months ago

        The example even used unwrap_or_else where they should use unwrap_or. Then it uses std::i64::MIN as fallback value where they could use something like 0 that would be a better example and honestly make more sense there.

        let parsed_numbers = ["1", "not a number", "3"]
            .iter()
            .map(|n| n.parse().unwrap_or(0))
            .collect();
        
        // prints "[1, 0, 3]"
        println!("{:?}", parsed_numbers);
        

        Even without trimming this to something less convoluted, the same functionality (with different fallback value) could be written in more readable form.

        Obviously in the context of the page something like this would make way more sense:

        maybe_number.unwrap_or(0)
        

        Or perhaps more idiomatic version of the above:

        maybe_number.unwrap_or_default()
        
        • @[email protected]
          link
          fedilink
          210 months ago

          I think you could even get rid of the iter() and the collect() since it’s a small fixed size array.

    • @hansl
      link
      410 months ago

      The Option type would have been a better example, and make it slightly less complicated.

      Option is an enum with two variants; None and Some(T). You can chain Options with operations, describing a Monad chain, which is kind of what this meme represent.

    • xor
      link
      fedilink
      English
      210 months ago

      Damn, they really just made that example as ugly as possible huh