In this step, we will initialize a basic rust project, where we can start building our simple Rust state machine.
To keep your code clean and easy to read, we use a tool called rustfmt
. To access all the latest features of rustfmt
we specifically use the nightly
toolchain.
To install rustfmt
for nightly
:
rustup component add rustfmt --toolchain nightly
To configure the behavior of rustfmt
, we will create a rustfmt.toml
file:
Create a new file in your project's root directory called rustfmt.toml
.
touch rustfmt.toml
Use the following rustfmt.toml
file to configure your formatting preferences.
# Basicedition = "2021"hard_tabs = truemax_width = 100use_small_heuristics = "Max"# Importsimports_granularity = "Crate"reorder_imports = true# Consistencynewline_style = "Unix"# Miscchain_width = 80spaces_around_ranges = falsebinop_separator = "Back"reorder_impl_items = falsematch_arm_leading_pipes = "Preserve"match_arm_blocks = falsematch_block_trailing_comma = truetrailing_comma = "Vertical"trailing_semicolon = falseuse_field_init_shorthand = true# Format commentscomment_width = 100wrap_comments = true
Run the code formatter using the following command:
cargo +nightly fmt
But since we already cloned the repository in a folder named dotcodeschool-rust-state-machine
during the setup process, we will use that.
You shouldn't see any changes this time around, but as you write more code, you will be able to see cargo +nightly fmt
make everything look pretty, consistent, and easy to read.
We recommend you run
cargo +nightly fmt
after every step!
Another popular tool in the Rust community is Rust Analyzer.
It provides many features like code completion and goto definition for code editors like VS Code.
However, to provide the full functionality that it does, Rust Analyzer needs to compile your code. For a small project like this one, this is not a problem, however working with a large project like Substrate / Polkadot-SDK, it is.
It is my personal recommendation that Rust Analyzer is not needed in this workshop, and generally you should not use it for Substrate development. However, this section might be updated in the future to include special configurations of Rust Analyzer which will work well with Polkadot SDK in the future.
However, if you would like to use it anyway, now is the right time to set it up.