Feel like everyone’s been telling me it’s the best thing since sliced bread. I’m just a hobbyist with like a single big project I’m maintaining but I’m starting to hate the code and idk maybe I should rewrite it in rust. Idk

  • Nibodhika@lemmy.world
    link
    fedilink
    arrow-up
    12
    ·
    15 hours ago

    Let me start by saying I love Rust and think it’s great. I’ve been writing some personal stuff in Rust and it’s all that I’ve been looking for.

    However there is some stuff you said that makes me think Rust won’t be something you would fully appreciate Rust. Firstly every project, regardless of language, when it becomes big it becomes a hassle to maintain, especially if you’re not experienced enough to have set up a good architecture from the start.

    But more importantly, a lot of Rust benefits are stuff you didn’t mention, and some of the stuff you mentioned is not that important.

    • Speed: Performance gains will be minimal, most of the time your server takes on an API call is read/write disk/Network, and that takes as long as it takes regardless of language. Sure synthetic benchmark will tell you it’s twice the speed for a hello world, but the moment you start relying on outside stuff the line gets murky.
    • Less resources: Again, unless you have at least 10 instances of your service or are trying to get it to run in very minimal hardware conditions, you’re not likely to see any meaningful improvement. Sure your service now uses half the amount of RAM, but the hardware where you’re running it likely has a couple orders of magnitude more RAM than that anyways.
    • Garbage collection: You say that like it’s a bad word, but at the same time admit to have problems with C/C++, whose main differences are related to that.
    • Compilation: I don’t think you’re taking compilation into consideration, from line change to retest we’re talking a couple of minutes depending on the change. That can be annoying really fast, especially if you don’t understand the reason. Also, the Rust compiler is EXTREMELY pedantic, it will not let you compile stuff that you think should work because there’s an edge case that you’ve forgotten to consider because it’s not something you cared about, e.g. you can’t have global mutable variables because that can cause race conditions on multi-threading applications.
    • Security: you didn’t mention this but it’s one of the largest advantages of Rust, things like the Option or Result types as well as the match statements and the borrow checker are amazing and unmatched by anything else out there.
    • The Rust price: Rust is not free, you just pay beforehand, you win execution time at the expense of compiling time, you save on runtime errors at the expense of pedantic code checks, etc, etc. Rust is excellent if you understand why, and are willing to pay that price. Rust is for people who when the compiler says “you can’t do this because it will cause problems with X edge case” say “of course! Thanks!”, if you think “ugh, that’s never gonna happen, stupid compiler” then it’s not for you. To add to that, Rust can become quite complicated because of it.
    • A different way of structuring: Rust does not have classes or any OOP paradigms, it’s different from other stuff you’ve used and requires changes in how you structure your hada and application. Whether that’s for the best or not is very personal, but it’s definitely different.

    Finally I would like to finish up saying that Rust has the most excellent documentation: https://doc.rust-lang.org/book/ read that, then maybe do these https://github.com/rust-lang/rustlings if you get to the error types and are not drooling over the language, it might not be for you.

    • droning_in_my_ears@lemmy.worldOP
      link
      fedilink
      arrow-up
      1
      ·
      13 hours ago

      Thanks for the detailed write up.

      One more reason I might not have mentioned is that Rust is low level and has a good developer experience. At least I heard. The whole compiler is your best friend thing.

      Idk I guess I’m hoping it will teach me those concepts better without making me frustrated in the way C++ and C did. Those feel like they’re excellent languages that were no doubt revolutionary in their time but are now lumbered with legacy and unintuitive things. Maybe it’s false hope. Rust certainly looks intimidating but everyone says the tools and docs are amazing.

      I’ve decided I’m gonna learn it for sure. Whether I rewrite the project or not I’ll decide later.

      • Nibodhika@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        6 hours ago

        Yeah, that’s a good approach, learn it and it’s one more tool under the belt.

        That being said, I think you need to have some good level of proficient in C/C++ before lots of the Rust things make sense. I’m not sure what frustrated you about C/C++, and if you’d like to ask questions about that I’m happy to try to answer them. But a lot of concepts are the same just shown under a different light. For example, on C/C++ you chose to use stack or heap by declaring variables or allocating memory and storing it in pointers, in Rust you don’t allocate memory directly, instead you use types that store information on Heap, e.g. Box. I think that’s a lot less intuitive, and requires you to have a good grasp of heap/stack before it makes sense, but it prevents you from doing stupid stuff you might do accidentally on C/C++ like store a pointer to a stack object after it goes out of scope.

        But I might be wrong, it might be that for me all of that made sense because I knew C/C++ but that to you it will make sense regardless, and might even teach you those concepts that are useful on C/C++ too.

        • droning_in_my_ears@lemmy.worldOP
          link
          fedilink
          arrow-up
          1
          ·
          2 hours ago

          Hmm. Well I tried to learn C++ as my first programming language when I was 12 so that’s probably the reason lol. I was just bad at everything at that time. I moved onto Java next, but maybe I should revisit C++.

          But if I had to name specifics these come to mind right now:

          • Using code across multiple files or libraries is really annoying. The preprocessor directives, I kinda get them but there’s always something that breaks and I don’t understand why. Also why isn’t there a package manager or something like that? Hmm maybe there is nowadays but I don’t know if there was back then
          • Still don’t know what a link error is or why it would randomly show up and then disappear if I restart visual studio
          • I know this is a meme but long hard to understand error messages in the case of C++ and no useful error messages at all in C
          • In C did you know that the order of items in a struct matters? I didn’t until I spent an hour debugging a segfault.
          • Obviously C doesn’t have strings. Accomplishing even the simplest things in C feels really hard. Like you’re on your own.

          I’m very sure all of these can be summed up as me being bad at the languages. Skill issue etc. and it’s true. I am bad at both.

          But the point is there’s a lot of things I don’t understand and that seem unintuitive to me. So it’s not fun, so I don’t use it. If you gave me a programming problem for fun and told me choose your language I would never choose C++ and certainly not C. I’d use Python, JavaScript or Java because they feel more fun to use and I can see progress faster. Same for my new projects. I’ve never tried to make anything more complicated than a command line program in C++ or C.

          At the same time I understand that higher level languages abstract a lot of things away from you and I really do wanna get better at understanding those concepts.

          Anyway thanks for attending my therapy session.

          • Nibodhika@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            2 hours ago

            You might want to take a look at C/C++ again at some point, you didn’t mentioned a single thing that I expected, I expected you to complain about memory allocation, double frees or stuff like that, instead you touched on lots of very accurate pain points for C/C++ that people who use it for years feel.

            • Preprocessor directives: I assume you mean macros like #ifdef _WIN32, macros will change the code before it gets compiled, so they allow you to write meta-code, as in code that writes code. Rust takes this to a whole new level, so you might want to understand the basic concept before looking into advanced Rust. That being said, I don’t think these are complicated in and of themselves, nor do I think you’re having problem with understanding what they mean, and it’s more likely a question of why, and the answer is that some stuff should happen at compile time, e.g. checking if you’re on Windows or Linux do define how you deal with paths or something. But the same can be extended to knowing if a given feature is enabled so you can compile only parts of the program. The lack of package manager is a pain, but C/C++ predate lots of those concepts, and there have been lots of attempts. Also if you’re using Linux your system already has a package manager so using some common standard like CMake for your projects makes it all “just work” (most of the time)… But yeah, this one is a pain, no question about it, and Rust solved it properly.
            • Essentially the compiler couldn’t find something, they shouldn’t appear randomly and that’s perhaps a VS bug, never used VS for C/C++ development so I’m not sure on that one, but whenever I had link errors I was forgetting an include or a library on my CMake.
            • What do you mean by errors? Compiler errors? You learn to read them with time, but yeah, Rust compiler errors are much nicer, although they can lead you into a rabbit hole by suggesting changes you shouldn’t use.
            • I do know that, it’s not intuitive if you’re thinking on a higher level, but if you think on low level stuff it makes absolute sense. I can’t think of why this would have caused you trouble needing debugging though, you could be occupying more memory than necessary, or you could be running into problems with unions, but I can’t think of why the order of a struct would make anything crash unless you’re naively converting between types by casting a pointer, which is a red flag on its own.
            • Well, C doesn’t need strings, what is a string? It’s just an array of characters. C tries to not impose anything on top of the very basics, so it makes sense it doesn’t have a string type, appending to a string or to an array of numbers is essentially the same, you just deal with strings more commonly. And this forces you to think on what you’re doing, e.g. my_str += "something" sounds like a very simple thing, but under the hood you’re allocating an entire new string, copying the content from my_str into it, appending something and then deleting the old one. C forces you to do that stuff manually so you have to be conscious of what you’re doing, string operations are not cheap, and you can gain a lot of performance by simply preallocating all you’ll need from the start. Rust is similar here, while they do have a String type, they make a very big difference between it and a slice of a string, so that makes you conscious of when you’re doing heap stuff.

            I spent years with C/C++ as my main language, and I can tell you that I would also not choose it for a random project. However I would also most likely not pick Rust either. Python is my go-to for “I need something quick”. Rust would be my go-to for “I need something robust”, or “I want to make sure this will work as intended”, it will mean a more tedious development cycle with a very slow iteration (much slower than C/C++, e.g. adding an enum value can mean lots of fixes on Rust, because most places where you’re dealing with that enum would fail to compile because you’re not taking the new value into consideration), but it will mean that every step will be solid (if it compiles after adding an enum value, I’m sure it’s being considered).

            Do learn Rust, it’s fun and personally I see a LOT of future in that language, but it also abstracts some of the concepts away while still requiring you to know them, e.g. heap/stack. I think learning C/C++ for the core concepts is better, but if you know those concepts Rust is a better language overall.

  • pinball_wizard@lemmy.zip
    link
    fedilink
    arrow-up
    7
    ·
    17 hours ago

    I’m going to answer some questions I’m hearing in you question:

    "I’m interested in X, should I learn X?”

    The answer is pretty much always ”yes”, to that question.

    "Is Rust a good/terrible fit for my project?”

    Rust is a solid tool. It’ll probably do fine.

    • CanadaPlus@lemmy.sdf.org
      link
      fedilink
      arrow-up
      4
      ·
      edit-2
      16 hours ago

      I mean, there’s a big gotcha on:

      Is Rust a better fit for my project to the point it’s worth starting over?

      and

      Will I like my code better if it’s in Rust?

      That being said, it’s not necessarily a no on either, it just could very well be depending on things not disclosed in OP.

  • Valmond@lemmy.world
    link
    fedilink
    arrow-up
    10
    ·
    20 hours ago

    C/C++ senior developer here.

    If you don’t know, learn Python.

    There are very few things python can’t do on a hobby level. It’s simple and have tons of libraries and tutorials and so on. Also, no need to set up a compiler or so, it just runs straight out of the box. PyCharm is a simple and elegant IDE with debugging, but you could start out with the simplest text editor just to check things out.

    HTH

      • Valmond@lemmy.world
        link
        fedilink
        arrow-up
        3
        arrow-down
        1
        ·
        19 hours ago

        Just freeze the python to an executable is one way to do it (windows, linux, …).

        But now I’m curious, what’s your hobby project about? I mean you can close to chose any language and do anything … Do you do database? Graphics? Is it supposed to be online? Cryptographics? Gpgpu? Massive multithreading? Large calculations?

        If you don’t give us more information then we can only tell you to chose the language you like or master the most IMO.

        • droning_in_my_ears@lemmy.worldOP
          link
          fedilink
          arrow-up
          2
          ·
          18 hours ago

          It doesn’t really need it. I’m sorry for giving the impression that it’s some performance critical application lol.

          It’s just a simple web app backend with a db. Oh and the front end desktop version I wanna build will use tauri (because I hate the thought of bundling a browser) so that’s another reason to learn rust.

          I guess I just kinda want to make things minimal in the resources they use. Because that’s just neat :D

          • bluGill@fedia.io
            link
            fedilink
            arrow-up
            3
            ·
            18 hours ago

            There is a reason a lot of web apps run on the JVM - while it does have high overhead, once your app is running it is fast and you get some nice advantages. Plus Java has a large system of helpers and libraries. Even though you can do everything in Rust, you will end up writing a lot more code just because in Java you can just download a library to do something while Rust forces you to write it.

  • bluGill@fedia.io
    link
    fedilink
    arrow-up
    5
    ·
    18 hours ago

    Hating the code is normal. After a while you start to realize all the hard to change decisions you made long ago. What you don’t know is if those decisions you wish you had made would be better. There have been a lot of fads over the decades, some of them are complete junk, but the majority have some good points. However all of them have some negatives as well, and there is no obvious answer to which negative is right to accept for your projects.

    The ideal answer is spend 20 years learning lots of different options, then find a time machine and go back in time to and restart the project based on what you have learned. Of course at the end of that 20 years you will hear about some new thing you didn’t try, but you have to draw the line somewhere. (finding a time machine is left as an exercise for the reader)

    Rust is an interesting language with some nice memory safety guarantees. As a C++ developer it speaks to some of the problems I have - but most of my problems with C++ are with 15 year old code from before I was able to use C++11. ( had to put a lot of effort into getting this good with c++ though, Rust is likely a lot easier to learn to my level). The Ada/Spark advocates have long been saying things that really speak to me as well - formally proven code sounds great, though there are others who tell me it isn’t as good as the advocates claim. Go has some advocates saying interesting things as well, though they don’t speak to the issues I personally have as much it might be better for you.

    If you are writing all new code then I would put Rust high on the list. However most programs are adding to something that exists and the friction of writing Rust to existing code is often high enough that I’d stick with what the other code is written in.

      • bluGill@fedia.io
        link
        fedilink
        arrow-up
        2
        ·
        15 hours ago

        A part of it is when you get old you realize the “kids” are doing the exact same thing you did 20 years ago, but they gave it a different name and changed some tiny details so they can pretend it is different. The real question is how do you get kids to see this - most don’t (including me when I was a kid - and I was aware of the issue and tried to guard against it)

        The more important part at my age is to remember that kids today are just like me. Sure the exact style and what is in/out has changed, but kids are the same as we were. I didn’t even have the option of a smart phone, but I had my own addictions, and the older generation complained about them… Sometimes the old people have a point, but often kids these days are just like kids in the past.

  • sbv@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    6
    ·
    20 hours ago

    Give it a shot!

    Rust can be a real pain in the ass: it’s like having to solve mini logic puzzles as you’re writing your code. At best, it’s fun, at worst, it’s a distraction and source of frustration.

    You can always stop the rewrite if you discover you don’t like Rust.

  • Lucy :3@feddit.org
    link
    fedilink
    arrow-up
    2
    ·
    16 hours ago

    If you either hate yourself, or somehow manage to forget C/C++, JavaScript, Python and probably some more random languages, then yes. Because Rust is basically just a confusing, illogical mix of all of that. Otherwise, as others pointed out, learn anything else that is - if that’s what you want - memory safe, statically typed, yadda yadda yadda, and has a good dependency system, compiler and syntax. Other languages could very well fit much better too, depending on available libraries and features in general.

    • Nibodhika@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      15 hours ago

      While I agree with you that OP shouldn’t migrate to Rust, the language is not illogical nor a mix of any of those. There’s a lot of stuff that Rust does extremely well that can’t be reproduced in any of the other languages, such as Options or Result types. And while it can become confusing when lifetimes get thrown at it as a general rule it’s not. Also the compiler messages are so thorough, I don’t think I have ever seen anything come close to it.

      Rust is not for everything, nor for everyone, but saying it doesn’t have it’s merits is dishonest.

  • donut_delivery@lemmy.dbzer0.com
    link
    fedilink
    English
    arrow-up
    3
    ·
    20 hours ago

    That depends from the language you’re using now. To really appreciate and understand Rust’s borrow checker you need to have some experience in C or similar language. For someone who have used only Python before Rust will look really weird and messy

    • droning_in_my_ears@lemmy.worldOP
      link
      fedilink
      arrow-up
      1
      ·
      20 hours ago

      I used C++ and beriefly C before. I suck at both lol.

      I get that manually managing memory can be a mess and easily create big problems. Garbage collectors seem like a cool solution but they need a runtime. Rust has no runtime but somehow forces you to manage memory well. Idk how.

      But anyway more important to me is that it’s a modern language with good devex and tools and no runtime. So it’s like if C++ was remade today. At least that’s what I hope.

      • sbv@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        6
        ·
        20 hours ago

        Rust has no runtime but somehow forces you to manage memory well. Idk how.

        You tell the compiler which part of your code is responsible for each chunk of memory. Then the compiler gives you errors when you don’t respect that. Roughly.

      • CanadaPlus@lemmy.sdf.org
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        15 hours ago

        Garbage collecting will make your life easier if you don’t need (or just want, as a hobbyist) fast code. It’s often worth it. Based on your other answers it sounds like you want the conceptual cleanness of not using it, though, which I totally get.

        If so, Rust is the game in town right now. The caveat is that you might put all the effort in and still hate your code. And be even further away from finishing it.

        So it’s like if C++ was remade today.

        It stuck around for decades because short of memory unsafety, there’s relatively few things wrong with C/C++. Rust was the first language that managed to do what Rust does, because it’s a hard problem. So basically, there isn’t any recommended third option.

        The way human programmers think is very different from the way processors do. You either insert a runtime that can bring the two closer together, including garbage collection, or expose the difference to the coder. C exposes the difference as invisible memory leaks, segfaults and occasional undefined behavior. Rust exposes it as lifetimes and borrowing, with complicated enough rules most users just change things until it works.

      • donut_delivery@lemmy.dbzer0.com
        link
        fedilink
        English
        arrow-up
        3
        ·
        20 hours ago

        If you suck at C, then switching to Rust will not magically change it. C is a small language and learning it is easier than learning Rust. Also, you probably suck at C++ because you started learning it before C.

      • insomniac_lemon@lemmy.cafe
        link
        fedilink
        English
        arrow-up
        1
        ·
        16 hours ago

        Maybe check out Nim-lang? It does have mm (I hear arc/orc are good) though it can be turned off allowing you to manage memory manually.

  • solrize@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    18 hours ago

    What is the code written in now? What happens if you clean it up? What is the project if I can ask? Everything has a runtime, though some are more complex than others. You might like OCaml which is where Rust got some ideas, or Haskell if you like brain benders. Rust (like C) is for when you need very efficient code, and you incur some pain in order to get that.

    • droning_in_my_ears@lemmy.worldOP
      link
      fedilink
      arrow-up
      1
      ·
      18 hours ago

      The code is in Typescript. It’s the backend of a web app that’s all. Doesn’t really need to be in Rust but idk I just want to make something that doesn’t use more resources than it needs to. Just to be neat :D

      • solrize@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        13 hours ago

        You might give Go (Golang) a try. It’s lighter weight than Java, pretty fast, and easy to use.

      • CanadaPlus@lemmy.sdf.org
        link
        fedilink
        arrow-up
        1
        ·
        16 hours ago

        For certain backends, especially the kind that generates static pages, functional code like Haskell works well, and brings a different kind of “cleanness”.

        Just in case you hadn’t considered it.