• marcos@lemmy.world
    link
    fedilink
    arrow-up
    19
    arrow-down
    4
    ·
    20 小时前

    have to check and destructure the result of every function call

    Learn how to use enum error types, how error bubbling works, and how to convert between Options and Results.

    It’s Rust you are talking about, not Go.

    • rtxn@lemmy.world
      link
      fedilink
      arrow-up
      5
      arrow-down
      7
      ·
      20 小时前

      This isn’t about some feature of the language being good or bad. It’s about Rust being ugly or not. The things I mentioned will always look ugly in the source code.

      • 5C5C5C@programming.dev
        link
        fedilink
        arrow-up
        9
        arrow-down
        1
        ·
        14 小时前

        It’s hilarious to me that people talk about “ugly” as if their opinions are objective.

        I found Rust unpleasant to look at for the first two weeks of learning it, and now that I’ve been using it professionally for three years I loathe when I need to read code in other languages.

        No other language can rival Rust in showing the exact information needed to understand the code — never too much and never too little — while being concise, correct, and handling all edge cases.

        You can be more concise in other languages, but it will come the loss of handling every little possible bug. You can be prettier in other languages, but it will come at the price of adding a lot of useless boilerplate.

        Of course there are cases where Rust can be verbose or confusing, but that’s when you’re doing very esoteric things that would be just as confusing in other languages.

        Like any opinion on aesthetics, how someone feels about the prettiness of a language will have far more to do with familiarity than with any objective metrics.

    • balsoft@lemmy.ml
      link
      fedilink
      arrow-up
      4
      arrow-down
      11
      ·
      edit-2
      20 小时前

      Sadly there’s still no truly good way to handle errors in rust. TBH I’m not sure if we as an industry have figured out an efficient, foolproof, composable, not overly verbose way to handle errors, so it’s not entirely on Rust.

      • calcopiritus@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        23 分钟前

        Rust allows you to choose whatever method you want.

        • Early return propagating the error
        • Early return ignoring the error (maybe by returning a default value)
        • Explicit handling by if-else (or match) to distinguish between error and not error cases.
        • Early return and turn the error into another type that is easier to handle by the caller.
        • Assume there is no error, and just panic if there is. (.unwrap)

        There are only 2 error handling methods that you cannot do:

        • Exceptions
        • Ignore the error and continue execution

        And that is because both of them are bad because they allow you to do the second one, when .unwrap is just there and better.

        If your concept of “not ugly” is “I just want to see the happy path” then you either write bad code that is “not ugly” or write good code that is “ugly”. Because there is no language that allows you to handle errors while not having error handling code near where the errors are produced.

      • Ephera@lemmy.ml
        link
        fedilink
        English
        arrow-up
        2
        ·
        14 小时前

        Yeah, I was gonna say, error handling easily makes up 80+% of the code paths, and depending on whether you’re building a library or service or script etc., different strategies are most suitable for how to deal with those code paths.

        In a script, you often just want it to crash. In a library, you want to make these code paths matchable, in case the user cares why something failed. And then you have the awkward in-between, which is that 99% of your application codebase will be used by your main-function like a library, but you don’t want to spend as much effort on error handling for that as a proper library does, in particular also because you know what all consumers of your application-library need to know.

        So, it’s kind of multiple different problems, with overlap, and people are hoping for one easy solution to cover all these problems.