Learning Rust

This post is here to help you get the working knowledge of Rust when you start developing Holochain applications.

learning resources

unusual Rust features

If you can only focus on a few things, this is the part you should read. These features and their syntax are very specific to Rust and are responsible for most of the confusion among the language learners.

  • ownership memory model
    • TL;DR: there’s no garbage collection and you don’t manually make calls to free memory. Instead, you use syntax to specify which function would have to (automatically) free memory for which variable. In other words, which function is the variable’s owner.
    • proper explanation from the official doc: What is Ownership? - The Rust Programming Language
  • string implementation (&str and String data types)
    • TL;DR: string literals &str are immutable and compile time defined; String data type is mutable and run time defined. This separation simplifies memory management.
    • proper explanation from the official doc: What is Ownership? - The Rust Programming Language
  • there is no built-in None/nil/null value
    • TL;DR: it’s impossible for a variable to not be initialized: it either has a value corresponding to it’s type or doesn’t exist. But you can define variable using special enum Option that implements a None value.
    • proper explanation from the official doc: Defining an Enum - The Rust Programming Language
  • error handling with Result enum and ? symbol after function calls
    • TL;DR: if your function can result in an error, you can define it’s return value using the Result enum and specify data type of successful and failed function result. Every time you see ? symbol in the code, it means that function preceding it returns some kind of Result.
    • proper explanation from the official doc: Recoverable Errors with Result - The Rust Programming Language

Rust features that you should be familiar with

Rust features you can ignore for now


P.S. if you have ideas on short explanations for Rust features that would help people transition from other languages – please let me know! :slight_smile:

9 Likes

if one is just learning to code now, and is very intrigued by the holochain project (let’s say a young high school student)

would learning rust first be advisable? what path would you recommend? learning other programming languages first?

… ?

@spirit hi there :slight_smile:

I don’t have a definitive answer here, instead I have a pros/cons list for learning Rust as your first language:

  • pros
    • you will likely have a deeper, more systematic view of how things work (especially when it comes to memory management), because with Rust you won’t have much of a choice
    • you will be able to write a bunch of code for multiple layers, from low-level OS stuff to cli tools to web backends and REST APIs
    • Rust code practices would likely help you develop efficient development habits which will be useful regardless of your language
    • being a beginner, you’ll shoot yourself in the foot far less frequently because Rust compiler will have your back with detailed error messages and examples. So even though your progress might be slower, it will be more reliable and you’ll feel more confident in the code that compiles (once it does, hehe)
  • cons
    • your first results will likely come later than if you’ve chosen let’s say JS/Python, so you might feel discouraged at the start and slow down. Consider this point if you’re used to fast feedback. If you just want to start building something for the sake of results (which is more than fine!), starting with something less complex will be a good choice.
    • there will be occasional things that Rust & community haven’t figured out just yet, which would mean there won’t always be a plethora of solutions like with more widely used languages. It’s not too bad though, because there are a lot of places to ask for help.

Hope it helps!

4 Likes

I can suggest this website for practising Rust. Rust | Exercism

Like rustlings, it’s exercises to achieve for learning important aspects of the language. All the exercises are very good for practising common math, algorithms and data structure principle that we can find in almost every language too !

We can have free mentorship and review the solutions of the community as well, what is very valuable I think !

4 Likes