Thanks to visit codestin.com
Credit goes to programming.dev

    • Ephera@lemmy.ml
      Codestin Search App
      Codestin Search App
      English
      Codestin Search App
      6
      ·
      19 days ago

      To be honest, I think, they both have their place. In Rust, you typically wouldn’t return just a bool, but rather the element that you removed, so like this:

      fn getofmylawn(lawn: Lawn) -> Option<Teenager> {
          lawn.remove()
      }
      

      And then with such a more complex return-type, C-style means that you can’t see the function name right away:

      Option<Teenager> getofmylawn(Lawn lawn) {
          return lawn.remove();
      }
      

      I also really don’t think, it’s a big deal to move your eyes to the ->

          • fruitcantfly
            Codestin Search App
            Codestin Search App
            Codestin Search App
            2
            ·
            18 days ago

            I believe that it is useful in a few places. cppreference.com mentions templates as one case:

            Trailing return type, useful if the return type depends on argument names, such as template<class T, class U> auto add(T t, U u) -> decltype(t + u); or is complicated, such as in auto fpif(int)->int(*)(int)

            The syntax also matches that of lambdas, though I’m not sure that adding another way of specifying regular functions actually makes the language more consistent, since most code still uses the old style.

            Additionally, the scope of the return type matches the function meaning that you can do

            auto my_class::my_function() -> iterator { /* code */ }
            

            instead of

            my_class::iterator my_class::my_function() { /* code */ }
            

            which is kinda nice