Table of Contents

February 27, 2022 5 min read

My Tryst with Building a Notes App in Rust on NEAR: Part 1 of 2

A detailed overview of how to get started with Rust

In my previous article, I covered the basic concepts needed for developing on blockchain. In this tutorial, I'll try articulating my process as I go about building a notes storing app in Rust which will be deployed over the NEAR protocol.

Rust is the preferred programming language for writing smart contracts on NEAR. It is type safe, memory safe, and free of undefined behaviors.Through compiler flags, Rust can automatically protect against integer overflow. In addition, Rust has an integrated test and benchmark runner. Rust reorders struct fields in order to make each type as small as possible, thus optimising for efficiency.

Most importantly, the smart contracts on Rust compile to WebAssembly. WASM is becoming a standard language for web applications. It surely will provide a robust foundation for DApps in the future.

Without further ado, lets delve right in .

We'll cover the following topics in detail:

  1. Writing a blockchain app in Rust
  2. Setting up our Rust app
  3. Debugging through the app
  4. Putting it all together
  5. Testing our Rust blockchain
  6. Errors you might encounter

Writing a blockchain app in Rust

In this guide, I aim to provide a meticulous detailed overview of the code. I'll try to explain the errors I encountered, how I solved them and why they occurred. You wouldn’t be the first  developer to be intimated by a completely new tech stack. Trust me, I was in your shoes not too long ago. To gently dip our feet in, we'll build a very simple Notes storing simple from scratch.

This project, not remotely ready for a production use case, will purely serve as a starting point for experimentation and practical application. A canvas for you to start dabbling your ideas with,so to say, as you get more familiar with Rust, NEAR and blockchain tech in general.

Setting up Rust

1.Install Rustup

sudo apt install build-essential #Install pre-reqs
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Press 1 and then the enter key when prompted for installation.

2. Configure your current shell

source $HOME/.cargo/env #Import the environment config for rust
rustc --version

3.Setup Dev environment

Now that rust is successfully up and running, let us setup the Rust development environment using Visual Studio Code

a. Install Visual Studio Code

b. Install WSLand the Rust extension for VSCode

3. Setup Dev environment

Now that rust is successfully up and running, let us setup the Rust development environment using Visual Studio Code

a. Install Visual Studio Code

b. Install WSLand the Rust extension for VSCode

In case you are having trouble setting up WSL on your computer, you can also try out Google Cloud Shell.

Setting up Google Cloud Shell

https://shell.cloud.google.com/?show=ide%2Cterminal

Google cloud shell provides the command-line access to a virtual machine instance in a terminal window. It provides you 5 GB of space in your $HOME directory.  Many of the command-line tools such as the bash,sh, emacs and vim are already pre-installed and kept up to date with Cloud Shell.

In case you don' t want to install all these dependencies and just want to give NEAR a try, this would be the best approach, since you will not have to manage any of the pre-requisites as Google Shell does it for you :)

4. Add wasm target to your toolchain

To set up the project, we need to add the WebAssembly (WASM) to our toolchain.

Basically to deploy our smart contract on NEAR, we need to compile it to a WebAssemble ( .wasm) file. The rustup command installs the standard libraries for the WebAssembly.

rustup target add wasm32-unknown-unknown

5. Install near-cli by running

Install node ( Not required if you are using Google cloud shell)

Install near-cli by running

npm install -g near-cli

6. Say hello to Cargo

Cargo is Rust’s build system and package manager.It basically makes your life much easier. Cargo handles a lot of tasks like building your code, downloading the libraries your code depends on etc.

Cargo comes installed with Rust. So, you can check whether Cargo is installed by

This is where the real fun begins!

  1. Let's start by creating our Rust project:
cargo new test101
cd test101

The first command creates a new directory called test101. We’ve named our project test101, and Cargo creates its files in a directory of the same name.

Go into the test101 directory. You’ll see that Cargo has generated two files and one directory for us: a Cargo.toml file and a src directory with a main.rs file inside. I'll be deleting the main.rs because we don't need it for this project.

It has also initialized a new Git repository along with a .gitignore file. Git files won’t be generated if you run cargo new within an existing Git repository; you can override this behavior by using cargo new --vcs=git.

Now, edit the Cargo.toml file and this file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format, similar to a package.json file. Replace the content with the following

[package]
name = "test101"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = "3.1.0"

[profile.release]
codegen-units = 1
opt-level = "z"
lto = true
debug = false
panic = "abort"
overflow-checks = true

In Rust, packages of code are referred to as crates. To know more about cargo visit the documentation

Creating the files

Smart contracts from NEAR usually have a primary file that holds the code: ./src/lib.rs. This is the conventional filename for a Rust library. Libraries will work great for compiling into WebAssembly and deployed the blockchain. [src]

A NEAR contract written in Rust must be compiled as a library. On NEAR, contracts run inside a virtual machine called the NEAR VM. All we need to know about NEAR VM is that it is optimized to work with the blockchain.

You can clone the repository

Errors you might encounter

1.

Solution : run sudo apt install build-essential # Install pre-reqscurl on your wsl terminal or visit

2.

Solution : run sudo apt install build-essential # Install pre-reqscurl on your wsl terminal

3.

Solution : Run sudo npm install -g near-cli

4.

Solution : Uninstall rust extension -> Run rustup self uninstall -> Install rust again -> Install the rust extension

Resources

  1. https://www.loom.com/share/fb4dc86376774857b34d3f5005c474a3
  2. https://www.near-sdk.io/contract-structure/collections
  3. https://learnnear.club/near-smart-contracts-in-rust-best-practices/
Great! Next, complete checkout for full access to Crypto Capable.
Welcome back! You've successfully signed in.
You've successfully subscribed to Crypto Capable.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.