Skip to content

Shawn: Introduction to Rust (for Substrate / Polkadot) #134

@shawntabrizi

Description

@shawntabrizi

In Progress


This issue will capture my thoughts on the different topics that an introduction to Rust for Substrate and Polkadot should have.

I have explicitly not chosen to look at the existing content, and work from my own experience to compile a list of content that I think should be covered.

I will use the Rust Book as a basis for the content that people learn outside of Substrate and Polkadot.


I would start by having users go through chapters 1 - 4 of the Rust Book on their own. I don't think there is much value to reproduce this content on our side, as we don't have much to expand on, and it would end up being copy paste.

I do think we need to have our own "installation" section, and basic commands needed to keep rust working in our ecosystem:

  • rustup update (always)
  • install wasm target
  • cargo fmt
  • etc...

I think we should also give a nice and clear warning that rust-analyzer does work, but not super well. We should probably include a special section on specifically configuring rust-analyzer for Substrate projects.


There needs to be a special section which specifically talks about the wasm32-unknown-unknown target, and no_std. This should be framed as entirely Rust things, which are then almost always used in Substrate.

Many of the examples throughout teaching Rust will use things like println!, rand, and many other no_std incompatible apis. They should already be prepared to understand that these tools are not available to them and why.

This should cover the intricacies of adding new crates to a no_std project. Specifically the TOML file, and errors that can result from that.


We need special emphasis on panics, what kinds of code triggers a panic (unwrap, overflows, etc...), and how we can always write Rust in panic-safe ways.


Need special emphasis on floating point numbers, and how they are not useful in blockchain environments.

We should teach how we can use unsigned integers and create fixed point numbers.

Special emphasis on safe math operations, and how rust can "hide" overflows in productions.


We should have a special section about Strings in Substrate, how they are represented as UTF-8 characters in a Vec<u8>.

Rust book has an entire section on Strings, and we should emphasize you will likely never touch a real string type in Substrate. Or in blockchains in general.

This might bleed into SCALE, which I feel like it might need to bring into these lessons.

https://substrate.stackexchange.com/questions/1174/why-is-it-a-bad-idea-to-use-string-in-an-ink-smart-contract


Also a SCALE-ish topic, we should emphasize, along with the different data types and structures which are equal and not equal encoded.

For example, tuple and struct can be equal. Two structs where field order is different is not equal.


Special emphasis on designing systems with a builder pattern.

Private vs Public fields in structs, etc...

probably kind find some tangible examples of where to use builder pattern.


Special emphasis on different ways to structure projects.

  • start with a large project in a single file
  • create multiple impls on a trait
  • create multiple internal modules
  • move those to sub-files and sub-folders
  • handling importing of mods

Special emphasis on feature flags and feature gating. Specifically creating a new feature flag, no_std ,and tests feature flags.

explaining: #![cfg_attr(not(feature = "std"), no_std)]

https://substrate.stackexchange.com/questions/1274/why-do-we-need-cfg-attrnotfeature-std-no-std


Special emphasis on creating macros

  • #![recursion_limit = "1024"]
  • different kinds of macros, how they work, what they do, how they are useful, etc...

https://danielkeep.github.io/tlborm/book/README.html


Bounded vectors and other "bounded types". This certainly bleeds into SCALE, which probably should be here.

If we touch on scale, we should talk about what it does and how it works and stuff.


Generics and associated types.

When to use each.

https://github.com/shawntabrizi/substrate-trait-tutorial


fancy use of cargo

  • build a specific package
  • run a specific test
  • debug vs release vs production builds

Talk briefly about Box and how we use it in Wasm and other memory limited environments.


Crates interacting with each other through traits

  • using a third, "library" crate to connect two crates
  • showing problems with recursive dependencies

Conflicting associated type names, and how to resolve the rust error.

Inferred types in general, and how they work.

Processes for debugging this.


passing iterators vs vectors / slices to functions

working with iterators in general


rust modules vs rust libraries

in substrate, you mostly work with libraries

Metadata

Metadata

Assignees

No one assigned

    Labels

    Content AdditionSuggestions for new content additions.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions