DEV Community

Mathijs Vogelzang
Mathijs Vogelzang

Posted on

3 things you have to get used to when learning Rust

I've been using Rust for over half a year now. It's an amazing technology. Nevertheless, there are some things that keep tripping me up. The following are the three things that come to mind:

  1. If statements have no parentheses

    if language=="Rust" {
      println!("Don't use parentheses!");
    }
    

    As I frequently switch back and forth still with TypeScript and
    Kotlin, I keep having to get used to not typing parentheses for the expression of if statements. And then, when I've been doing
    Rust long enough, I have to get used to adding them again when
    switching back to TS/Kotlin 😫.

  2. async code still needs careful attention
    A big benefit of Rust is that it has the speed of non-garbage collected languages like C/C++ but still has the same memory safety as Java/Python/Go and others. Concurrent programming is also safe; the compiler makes it nearly impossible to create a race condition.
    If you start writing async code, this safety is also not fully there. We ran into issues where an accidentally long-running task was scheduled like
    tokio::spawn(async || sync_function_that_sometimes_runs_long()), and when a couple too many of these tasks got executed, they blocked all tokio functionality from running at all.
    The fix is to run these tasks with tokio::spawn_blocking, but you have to think of that yourself; the compiler doesn't check this for you.

  3. Your target/ dir needs to be occasionally cleaned up
    Rust fills the target dir with intermediate results, and this directory grows very big quickly. (Multiple GBs for a trivial project, and after more time working on a project, it can easily become 20GB).
    My VSCode sometimes stops monitoring files in my workspace because the target dir is so big. (This can be fixed by adding target to the files.watcherExclude setting, see this documentation page for more information).
    Even with VSCode ignoring target, it's still a good idea to clean occasionally. Otherwise, you might run out of disk space if you have more than a handful of Rust projects!

What do you think are the biggest gotchas with Rust?

Top comments (0)