Rust program
Rust Program Quickstart
Rust is the most common programming language to write Huione programs with.
This quickstart guide will demonstrate how to quickly setup, build, and deploy your first Rust based Huione program to the blockchain.
NOTE: This guide uses the Huione CLI and assumes you have setup your local development environment. Checkout our local development quickstart guide here to quickly get setup.
What you will learn#
How to install the Rust language locally
How to initialize a new Huione Rust program
How to code a basic Huione program in Rust
How to build and deploy your Rust program
Install Rust and Cargo
To be able to compile Rust based Huione programs, install the Rust language and Cargo (the Rust package manager) using Rustup:
Run your localhost validator
The Huione CLI comes with the test validator built in.
This command line tool will allow you to run a full blockchain cluster on your machine.
PRO TIP:
Run the Huione test validator in a new/separate terminal window that will remain open.
This command line program must remain running for your localhost validator to remain online and ready for action.
Configure your Huione CLI to use your localhost validator for all your future terminal commands and Huione program deployment:
Create a new Rust library with Cargo
Huione programs written in Rust are libraries which are compiled to BPF bytecode and saved in the .so format.
Initialize a new Rust library named hello_world via the Cargo command line:
Add the Huione-program crate to your new Rust library:
Open your Cargo.toml file and add these required Rust library configuration settings, updating your project name as appropriate:
Create your first Huione program
The code for your Rust based Huione program will live in your src/lib.rs file.
Inside src/lib.rs you will be able to import your Rust crates and define your logic.
Open your src/lib.rs file in your favorite editor.
At the top of lib.rs, import the huione-program crate and bring our needed items into the local namespace:
Every Huione program must define an entrypoint that tells the Huione runtime where to start executing your on chain code.
Your program's entrypoint should provide a public function named process_instruction:
Every on chain program should return the Ok result enum with a value of ().
This tells the Huione runtime that your program executed successfully without errors.
This program above will simply log a message of "Hello, world!" to the blockchain cluster, then gracefully exit with Ok(()).
Build your Rust program#
Inside a terminal window, you can build your Huione Rust program by running in the root of your project (i.e. the directory with your Cargo.toml file):
NOTE: After each time you build your Huione program, the above command will output the build path of your compiled program's .so file and the default keyfile that will be used for the program's address.
Deploy your Huione program
Using the Huione CLI, you can deploy your program to your currently selected cluster:
Once your Huione program has been deployed (and the transaction finalized), the above command will output your program's public address (aka its "program id").
Congratulations!#
You have successfully setup, built, and deployed a Huione program using the Rust language.
PS: Check your Huione wallet's balance again after you deployed. See how much Huione it cost to deploy your simple program?
Next steps
See the links below to learn more about writing Rust based Huione programs:
Last updated