• pewpew@feddit.it
    link
    fedilink
    arrow-up
    2
    ·
    52 minutes ago

    C is the way.3̶̧̧̳̉ẻ̵͙̗͍͒h̶͈̗̊͘o̷̡̳̥̒͐̇f̷͍̳͕̐{̸͇̀̒?̷̤͇̀̊p̴̰̆̍̕

  • Lovable Sidekick@lemmy.world
    link
    fedilink
    English
    arrow-up
    9
    ·
    edit-2
    2 hours ago

    The old school method of learning a programming language, database, framework or whatever was to read books and take classes, do a series of exercises that teach you how to use the features, and the errors you get if you don’t do it right. Then you write code that way for like 10-15 years.

    The Information Age method is to find some sample code, copypaste into an editor and hit Compile, then paste compile errors into google and fix them until there are no more. Then hit Run and copypaste/fix runtime errors until there are no more runtime errors. Old-schoolers used to call this hacking, but now it’s called not having time to deeply learn the hot new thing because before you do you’ll have to start over with the next hot new thing.

  • diffusive@lemmy.world
    link
    fedilink
    arrow-up
    29
    ·
    11 hours ago

    Call me a weirdo but the more errors a compilers give me the happier (albeit a bit frustrated) I am. That stuff generally surfaces in a way or another… and I prefer at compile time 🙂

    That said I haven’t spent quality time with Rust yet… so not sure if there are a lot of nitpicks (ala go) or these are valgrind-level of “holy s*** I am so grateful to this tool” 😃

    • itslilith@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      2
      ·
      2 hours ago

      The borrow checker makes things a bit more complicated to get running, definitely takes some getting used to when you come from a non-memory safe language. But the compiler is really helpful throughout almost all mistakes, often directly providing an explanation and a suggested fix. One of my favorites programming experiences so far

      • qaz@lemmy.world
        link
        fedilink
        English
        arrow-up
        1
        ·
        edit-2
        1 hour ago

        …definitely takes some getting used to when you come from a non-memory safe language…

        I actually think it’s more like the opposite. The compiler takes the normal rules you apply to avoid issues with a non-memory safe language like C/C++ and enforces them explicitly where memory safe languages don’t have those rules at all. I think lifetimes are much more confusing if you’ve never dealt with a user after free and usually let GC deal with it.

        Also yes the compiler warnings and errors are amazing, the difference between rustc and gcc is night and day.

  • ExLisper@lemmy.curiana.net
    link
    fedilink
    arrow-up
    14
    ·
    edit-2
    11 hours ago

    In my experience rust compiler simply moves the errors to earlier stage of development. With rust I write something and get bunch of errors right in the IDE. I spend some time fixing those and when all the compilation errors are gone in 99% of cases the code works and does what it’s supposed to do.

    With other languages I write some code and the compiler/interpreter says it’s all good. I then run it, get bunch of errors and have to do some debugging, move back and forth between the editor and the command line/browser/application and fix all the bugs one by one.

    So yeah, rust compiler complains a lot but it’s to make your life easier, not harder. For me working rust way is just much more pleasant. I get immediate visual clues about the errors right in the IDE. When I finally get it right and all the errors dispersal it’s like solving a small puzzle. You know you got it and it feels good. With other languages you think you got it all the time only to find another bug when you run it. Doing it this way is much more frustrating.

  • tiredofsametab@fedia.io
    link
    fedilink
    arrow-up
    6
    ·
    18 hours ago

    A friend told me about rust around 8 years ago and this was very much my first experience (at least with &str and lifetimes and borrow errors).

  • kubica@fedia.io
    link
    fedilink
    arrow-up
    72
    arrow-down
    1
    ·
    1 day ago

    The weird part of rust is replacing straight forward semicolons from other languages with the more verbose .unwrap();.

    Just kidding, don’t lecture me about it.

    • marcos@lemmy.world
      link
      fedilink
      arrow-up
      24
      arrow-down
      1
      ·
      1 day ago

      The amount of people on the internet seriously complaining that both Rust error handling sucks and that .unwrap(); is too verbose is just staggering.

      • wise_pancake@lemmy.ca
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        8 hours ago

        I’ll be honest, when I was learning to program in Java I mostly just wrapped errors in an empty try catch to shut them up, with no regard for actually handling them.

        I assume most other learners do that too.

        • marcos@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          5 hours ago

          Java requiring you to write every exception that can happen in your code isn’t helpful.

          Explicit error types are great, but Java managed to make them on a way where you get almost none of the upside and is so full of downsides that indoctrinated a generation into thinking knowing your errors is bad.

      • magic_lobster_party@fedia.io
        link
        fedilink
        arrow-up
        19
        ·
        1 day ago

        I think the problem is that many introductory examples use unwrap, so many beginner programmers don’t get exposed to alternatives like unwrap_or and the likes.

        • Ephera@lemmy.ml
          link
          fedilink
          English
          arrow-up
          5
          ·
          20 hours ago

          Yeah, we onboarded some folks into a Rust project last year and a few months in, they were genuinely surprised when I told them that unwrapping is pretty bad. Granted, they probably did read about it at some point and just forgot, but that isn’t helped by lots of code using .unwrap() either.

    • ImplyingImplications@lemmy.ca
      link
      fedilink
      arrow-up
      9
      arrow-down
      2
      ·
      1 day ago

      Me, every time I try searching a Rust question.

      That’s easy. Just do:

      fn is_second_num_positive() -> bool {
          let input = "123,-45";
          let is_positive =
              input.split(',')
              .collect::<Vec<&str>>()
              .last()
              .unwrap()
              .parse::<i32>()
              .unwrap()
              .is_positive();
          is_positive
      }
      
      • shape_warrior_t@programming.dev
        link
        fedilink
        English
        arrow-up
        16
        ·
        1 day ago

        Can’t resist pointing out how you should actually write the function in a “real” scenario (but still not handling errors properly), in case anyone wants to know.

        If the list is guaranteed to have exactly two elements:

        fn is_second_num_positive_exact(input: &str) -> bool {
            let (_, n) = input.split_once(',').unwrap();
            n.parse::<i32>().unwrap() > 0
        }
        

        If you want to test the last element:

        fn is_last_num_positive(input: &str) -> bool {
            let n = input.split(',').next_back().unwrap();
            n.parse::<i32>().unwrap() > 0
        }
        

        If you want to test the 2nd (1-indexed) element:

        fn is_second_num_positive(input: &str) -> bool {
            let n = input.split(',').nth(1).unwrap();
            n.parse::<i32>().unwrap() > 0
        }
        
        • beeb@lemmy.zip
          link
          fedilink
          arrow-up
          4
          ·
          edit-2
          15 hours ago

          Even better to use expect with a short message of what the assumption is: “the string should contain a comma” if it ever panics you’ll know exactly why.

  • mholiv@lemmy.world
    link
    fedilink
    arrow-up
    35
    arrow-down
    3
    ·
    1 day ago

    Skill Issue.

    For reals though adopting a functional style of programming makes rust extremely pleasant . It’s only when people program in object oriented styles that this gets annoying.

    No loops, and no state change make rust devs happy devs.

      • mholiv@lemmy.world
        link
        fedilink
        arrow-up
        11
        ·
        edit-2
        1 day ago

        I mean yah. That’s what it takes. But like when I try to write code around Arc<_> the performance just tanks in highly concurrent work. Maybe it’s an OOP rust skill issue on my end. Lol.

        Avoiding this leads, for me at least, to happiness and fearless, performant, concurrent work.

        I’m not a huge fan of go-lang but I think they got it right with the don’t communicate by sharing memory thing.

        • PlexSheep@infosec.pub
          link
          fedilink
          arrow-up
          1
          ·
          1 day ago

          You mean mutex? Arc allows synchronous read only access by multiple threads, so it’s not a performance bottleneck. Locking a mutex would be one.

          • mholiv@lemmy.world
            link
            fedilink
            arrow-up
            3
            ·
            edit-2
            15 hours ago

            I mean it could be Mutex, or Rwlock or anything atomic. It’s just when I have to put stuff into an Arc<> to pass around I know trouble is coming.

    • AnarchoEngineer@lemmy.dbzer0.com
      link
      fedilink
      arrow-up
      5
      ·
      1 day ago

      I just started learning rust like two days ago and I haven’t had too many issues with OOP so far… is it going to get considerably worse as the complexity of my projects increases?

      • Ephera@lemmy.ml
        link
        fedilink
        English
        arrow-up
        9
        arrow-down
        1
        ·
        19 hours ago

        The thing with OOP, particularly how it’s used in GCed languages, is that it’s all about handing references out to wherever and then dealing with the complexity of not knowing who has access to your fields via getters & setters, or by cloning memory whenever it’s modified in asynchronous code.

        Rust has quite the opposite mindset. It’s all about tracking where references go. It pushes your code to be very tree-shaped, i.e. references typically¹ only exist between a function and the functions it calls underneath. This is what allows asynchronous code to be safe in Rust, and I would also argue that the tree shape makes code easier to understand, too.

        But yeah, some of the patterns you might know from OOP will not work in Rust for that reason. You will likely need to get into a different mindset over time.

        Also just in case: We are talking OOP in the sense of the paradigm, i.e. object-oriented.
        Just using objects, i.e. data with associated functions/methods, that works completely normal in Rust.

        ¹) If you genuinely need references that reach outside the tree shape, which is mostly going to be the case, if you work with multiple threads, then you can do so by wrapping your data structures in Arc<Mutex<_>> or similar. But yeah, when learning, you should try to solve your problems without these. Most programs don’t need them.

      • qaz@lemmy.world
        link
        fedilink
        English
        arrow-up
        6
        ·
        24 hours ago

        It will become more complex when you start needing circular references in your datastructures.

      • mholiv@lemmy.world
        link
        fedilink
        arrow-up
        5
        arrow-down
        1
        ·
        1 day ago

        You’ll be fine. You will learn the lifetime stuff and all will work out. It’s not that bad to be honest.

      • felsiq@lemmy.zip
        link
        fedilink
        arrow-up
        3
        arrow-down
        1
        ·
        1 day ago

        Worse in the sense of more errors, sure, but as you go you’ll pick up more of the rust patterns of thinking and imo it’s very worth it. It’s an odd blend and can be a bit verbose but I definitely prefer it to a pure OO or pure functional style language personally

  • Kamikaze Rusher@lemmy.world
    link
    fedilink
    arrow-up
    18
    ·
    1 day ago

    This is my experience every time I return to learning rust. I’m guessing that if I used it more often than once a quarter with hobby projects I’d stop falling into the same traps.

    • boonhet@sopuli.xyz
      link
      fedilink
      arrow-up
      12
      ·
      18 hours ago

      I find that the error messages themselves are a great tool for learning when it comes to Rust.

      • Kamikaze Rusher@lemmy.world
        link
        fedilink
        arrow-up
        5
        arrow-down
        1
        ·
        18 hours ago

        Eh, I’m not entirely sold on that idea.

        I think they do a good job of pointing out “this is a behavior/feature of Rust you need to understand.” However they can send you down the wrong path of correction.

        The compiler error mentioning static lifetime specifiers of &str demonstrates both. It indicates to the developer that ownership and scopes will play a significant role when defining and accessing data. The error though will guide them towards researching how to define static lifetimes and possibly believe that they will need to set this in their functions and structs. Each time you look at questions about this error in places like Stack Overflow with example code you’ll find suggested solutions explaining that a manually-defined static lifetime isn’t necessary to resolve the problem.

        • magic_lobster_party@fedia.io
          link
          fedilink
          arrow-up
          1
          ·
          3 hours ago

          Static lifetimes confused me when I started learning rust. The error message guides the developer to the wrong direction.

          It took me a while to realize that just using Arc is sufficient in most of those cases.

    • Ephera@lemmy.ml
      link
      fedilink
      English
      arrow-up
      7
      ·
      17 hours ago

      Yeah, these become a lot less relevant with routine.

      • Avoiding the main-thread panicking is mostly just a matter of not using .unwrap() and .expect().

      • String vs. &str can mostly be solved by generally using owned datatypes (String) for storing in structs and using references (&str) for passing into function parameters. It does still happen that you forget the & at times, but that’s then trivial to solve (by just adding the &).

      • “temporary value dropped while borrowed” can generally be avoided by not passing references outside of your scope/function. You want to pass the owned value outside. Clone, if you have to.

      • “missing lifetime specifier” is also largely solved by not storing references in structs.

      • Kamikaze Rusher@lemmy.world
        link
        fedilink
        arrow-up
        5
        ·
        14 hours ago

        The last two points are the kind of design advice I need to see. I’m probably so used to the C/C++ concept of passing by reference to prevent copies of complex data being generated that I forget how Rust’s definition of a reference is different.