This post is here to help you get the working knowledge of Rust when you start developing Holochain applications.
learning resources
- Get a quick intro into the language at Half-hour to learn Rust
- For extensive details, check out the official Rust book
- See how to work with language features at Rust by Example
- If you want a more interactive way to have a look at Rust, check out the interactive tour of Rust
- Try writing your own Rust code from your browser in Rust Playground
- Get a very practical language introduction by fixing series of small problems in the rustlings.
- If you are ready to test your knowledge of Rust details or just in the mood to get surprised, check out the Rust battle
- Check out what are the crates you can use in your project at Rust Community’s crate registry
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
andString
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
- TL;DR: string literals
- 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
- 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
- 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 ofResult
. - proper explanation from the official doc: Recoverable Errors with Result - The Rust Programming Language
- TL;DR: if your function can result in an error, you can define it’s return value using the
Rust features that you should be familiar with
- code structure: packages, crates and modules
- TL;DR: package is the highest level of hierarchy and it can contain several crates. Each crate can contain several modules. Modules can be either a single file or a directory.
- proper explanation from the official doc: Managing Growing Projects with Packages, Crates, and Modules - The Rust Programming Language
- Cargo tool: building the Rust code and dependency management
- TL;DR: Cargo takes care of calling compiler with all the necessary options for you. It also automatically downloads code dependencies.
- proper explanation from the official doc: Hello, Cargo! - The Rust Programming Language
- see more at: Introduction - The Cargo Book
- all variables are immutable by default (and that’s it! no TL;DR)
- proper explanation from the official doc: Variables and Mutability - The Rust Programming Language
- double and single quotes have different meaning when working with the character & string types
- TL;DR: char literals are specified with single quotes, as opposed to string literals, which use double quotes
- proper explanation from the official doc: Data Types - The Rust Programming Language
- enums and pattern matching syntax
- TL;DR: enums are not int values under the hood and pattern matching is a bit similar to switch/case operator. Also, enums can wrap another values inside themselves!
- Enums and Pattern Matching - The Rust Programming Language
- what are structs
- TL;DR: bunch of data fields stored together. Golang also has structs, and for OOP languages that would be data fields of a class.
- proper explanation from the official doc: Using Structs to Structure Related Data - The Rust Programming Language
- what are generic data types
- TL;DR: these are called generics in Java too and templates in C++. They allow to implement code once but use it for multiple data types.
- proper explanation from the official doc: Generic Data Types - The Rust Programming Language
- what are traits
- TL;DR: these are called interfaces in Golang and Java, abstract classes in Python and C++. They allow to guarantee existence of certain methods with specified signatures for structs that implement these
- proper explanation from the official doc: Traits: Defining Shared Behavior - The Rust Programming Language
Rust features you can ignore for now
- lifetimes
- there are not used in most of the hApps so you can skip it
- but if you want to learn more, read this documentation: Validating References with Lifetimes - The Rust Programming Language
P.S. if you have ideas on short explanations for Rust features that would help people transition from other languages – please let me know!