I’ve been using Rust for a few months now, writing rather more of it than I expected—though quite a lot of that has been thrown away as I’ve learned, improved what I’m writing, and taken some more complex tasks beyond what I originally intended.

I still love it and thought it might be good to talk about some of the important keywords that come up again and again in Rust. I’ll provide my personal summary of what they do, why you need to think about how you use them, and anything else that’s useful, particularly for people who are new to Rust or coming from another language

Without further ado, let’s get going. A good place for further information is always the official Rust documentation—you’ll probably want to start with the std library.

  1. const
  2. let
  3. match
  4. mut
  5. return
  6. unsafe
  7. use

  1. const – You get to declare constants with const, and you should. This isn’t rocket science, but do declare with const, and if you’re going to use constants across different modules, then do the right thing and create a lib.rs file (the Rust default) into which you can put all of them with a nicely named module. I’ve had clashes of const variable names (and values!) across different files in different modules simply because I was too lazy to do anything other than cut and paste across files when I could have saved myself lots of work simply by creating a shared module.
  2. let – You don’t always need to declare a variable with a let statement, but your code will be clearer when you do. What’s more, always add the type if you can. Rust will do its very best to guess what it should be, but it may not always be able to do so at runtime (in which case Cargo, the compiler, will tell you), or it may even not do what you expect. In the latter case, it’s always simpler for Cargo to complain that the function you’re assigning from (for instance) doesn’t match the declaration than for Rust to try to help you do the wrong thing, only for you to have to spend ages debugging elsewhere.

#rust #programming #developer

7 Useful Keywords from the Rust Library
3.25 GEEK