NEAR zero to hero: Building a Smart Contract in Rust



  • Rust toolchain
  • A NEAR account
  • NEAR command-line interface (near-cli)

Setting up the requirements​

  1. Install Rustup
  • Open a terminal and run
  • Wait for the command to finish running
  • Run

Creating a NEAR account

  1. Reserve an Account ID

2. Secure your account

  • Choose your account recovery method. For simplicity, in this tutorial you can select “Email Account Recovery”.

3. E-mail / Phone Number Account Recovery

  • Enter the account activation code that you received.

4. Success!

  • You just created a testnet account and received 200 Ⓝ! Upon recovery method confirmation you should be directed to your account dashboard.

Linux and mac-OS

  1. Install npm and node using a package manager such as nvm.
  2. Ensure you have installed Node version 12 or above.
  3. Install near-cli globally by running:


  1. Install WSL [click here]
  2. Install Node.js [click here]
  3. Change npm default directory [click here]
  • This is to avoid any permission issues with WSL
  1. Open WSL and install near-cli globally by running:

Creating the repository​

Creating the files

Editing Cargo.toml

  • Open Cargo.toml in your text editor of choice. This file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format, and is similar to a package.json file.
  • Next, replace the content with the following Cargo.toml file.


  • Open the ./src/ file in your text editor, and paste the content of the following file.
  • This example uses a file with the smart contract logic using a struct, the struct’s functions, and unit tests.

Breaking it down

Imports and initial code

The core logic: the struct

Unit tests

Writing a test

Test & compile

Test the code

Compile the code

Deploy the smart contract 🚀

Login with near-cli

Deploying the contract

Invoking the methods



Check counter value

Next steps

Versioning for this article

  • cargo: cargo 1.53.0 (4369396ce 2021–04–27)
  • rustc: rustc 1.53.0 (53cb7b09b 2021–06–17)
  • near-cli: 2.1.0



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Near Protocol Nigeria (NPK Guild)

NPK Guild is a Near Protocol Community based in Nigeria, aimed at educating and incentivising young Nigerians onBlockchain technology and the NEAR ECOSYSTEM