New to Rust, but really stuck. How should I approach it?

Hi all, absolutely new to Rust. Got into it after a lot of friends suggested (read: forced) me to check it out.

I'm in sort of a weird position, though. I have former experience in C# and Python, but most if not all of my programming experience has been in controlled frameworks or environments (for example, my C# experience is like 99% Unity3D dev) or in the case of Python, I've used it more as a tool to solve a particular problem statement without regard for speed or memory. Kind of a bad look, I guess.

However, Rust is kind of a blank slate for me. I don't have any particular problem I'm looking to solve that Rust does better, and all the articles on "why you should learn Rust" mention things like memory management or faster compile times; things that I've either never had to deal with myself or I didn't have to care about.

With that said, what should my approach to Rust be as someone who's mostly worked in the safety (and confines) of predefined frameworks?

2 Likes

More likely faster run time :stuck_out_tongue:. Compile times are on the high end (slow).

If I were to approach Rust from the beginning, I think it's good to understand the following:

  • stack memory and heap memory (in relation to sized and unsized types)
  • what an invalid pointer is (Rust protects you from doing this, mostly)
  • passing arguments by value vs by reference (this will help to understand borrowing)

Usually the compile messages are good enough to explain what's going on. If not, then the community is :v:.

2 Likes

I would really recommend reaching out on the forum or other community channels if you have trouble with something or even sometimes to have a look at something that does work, to review and point you in the right direction. There are a lot of really helpful people over here who are glad to help.

Rust is very good at keeping you from doing things with disastrous consequences, so while you are safe from memory errors with Rust, you are not safe from frustration ( definitely read that link when you run into such ) with what it tells you you aren't allowed to do. Getting another person to look at your code can really help when that happens.


They way everybody learns is different. The options on the rust website for learning, i.e. the book, and a couple example-based courses, are a good start.

Also, if you have something that you think would be interesting, useful, or fun that you want to start to try to make, maybe just give a shout-out on the forum to ask for pointers on where you might want to start.

My approach to Rust for the first time a few months ago was as follows:

  1. Get some coffee and roll my sleeves up.
  2. Install Rust
  3. Fire up Visual Studio Code
  4. Start writing some Rust, following along from the Rust book: The Rust Programming Language - The Rust Programming Language

Pretty much the same approach as any other language.

Problem is, I tend to get bored with all that reading after a while and start itching to create an actual, working, complete program. To that end I set about recreating some some of my former C/C++ and Javascript programs. Nothing very big. Basically this is driven by the desire to prove to myself that I can actually do in Rust what I know I can do in other languages. Soon I was creating new code for our production systems. Now all new development in our new company is done in Rust!

This is a bit of a problem as now there are whole areas of the Rust book I have not explored, having diverted time into actually using Rust for real work. And hence a lot of Rust language features I'm unaware of. So my code is probably not as "idiomatic" Rust as it should be and most likely not done in the best way. Still, it's all been up and running reliably for months now :slight_smile: I must make time for more serious Rust study at some point.

I'm not sure it makes much difference what other language/environment one is coming from. In the past I have done a lot of work in C/C++ and in the recent past mostly Javascript under node.js. I needed Rust to replace both of those in our systems. I wanted the performance of C for those places where we need it and the simplicity of Javascript but with robustness. Largely I have achieved that with Rust. All though I notice that Rust can start to look as mind bendingly impenetrable as C++ template meta-programming in some hands!

In short: Just get started. When you get stuck the folks here are very tolerant of dumb newbie questions and very helpful. Rust is a very rewarding exercise.

5 Likes

@ActuallyAcey - also worth noting that since you're coming from Unity, you're probably aware of their shift toward DOTS (data oriented technology stack) which includes things like safe threading via Jobs and data alignment via ECS

Rust is (among other things) inherently thinking and solving things in the same space, e.g. "how do we provide abstractions that are safe but as fast as possible" - so I think it's a pretty good time to delve into it even if you want to get a better understanding of how/why Unity needs to do this in order to stay competitive.

Most people reference documents like The Book, but while it's a really good resource I prefer a more hands-on approach to learning. So instead I'll start a project and learn new things when I need them.

My go-to project for getting familiar with a new language is to write a parser and interpreter for a lisp-like programming language. I like it as a project because it gives you a good mixture of data structures, parsing and string processing, tree operations. It also feels a lot more satisfying than the contrived examples you'll encounter in a book.

Another useful way to experiment with the language is using existing programming problems like Project Euler or Exercism.

Rust is actually really good for this sort of thing! For example, at work I'm using Rust to automate things like our release process (it's complicated), to generate pretty reports that non-programmers can use to get an overview of recent changes, and for processing data recorded while troubleshooting.

I'd definitely recommend trying to rewrite some of your existing Python tools in Rust. It'll be a useful experience, have actual effects on your productivity, and will introduce you to libraries like clap and structopt (command-line argument parsing), serde (serialization), and regex (regex) which are the gold standard in their respective fields.


Don't forget to post questions on the user forum when you get stuck, people are really nice and happy to point you in the right direction. Bonus points if you can provide examples and what you're expecting to happen so we can streamline the troubleshooting process.

You can also read through existing threads to learn more about the language or pick up best practices.

It's quite common to hit a wall early on when transitioning from garbage collected languages because you now need to worry about "lifetimes" (i.e. who owns what and how long is an object alive for). If this happens don't stress or beat yourself up, object lifetimes are a programming concept that never get explicitly taught elsewhere. Regardless of whether that's in a GC'd language (the GC looks after it for you) or a normal systems language like C or C++ (you learn after spending many hours troubleshooting random crashes and undefined behaviour).

3 Likes

Thank you all so much for the advice! I'd personally want to mark Micheal's answer since it feels the most spot on for what I'm exactly looking for. I hope to keep you all updated when I get down to business!

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.