Rust is the most common programming language to write Solana programs with. This quickstart guide will demonstrate how to quickly setup, build, and deploy your first Rust based Solana program to the blockchain.
This guide uses the Solana CLI and assumes you have setup your local development environment. Checkout our local development quickstart guide here to quickly get setup.
Lo que aprenderás #
- how to install the Rust language locally
- how to initialize a new Solana Rust program
- how to code a basic Solana program in Rust
- how to build and deploy your Rust program
Install Rust and Cargo #
To be able to compile Rust based Solana programs, install the Rust language and Cargo (the Rust package manager) using Rustup:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Run your localhost validator #
The Solana CLI comes with the test validator built in. This command line tool will allow you to run a full blockchain cluster on your machine.
solana-test-validator
Run the Solana 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 Solana CLI to use your localhost validator for all your future terminal commands and Solana program deployment:
solana config set --url localhost
Create a new Rust library with Cargo #
Solana 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:
cargo init hello_world --lib
cd hello_world
Add the solana-program
crate to your new Rust library:
cargo add solana-program
It is highly recommended to keep your solana-program
and other
Solana Rust dependencies in-line with your installed version of the Solana
CLI. For example, if you are running Solana CLI 1.17.17
, you can instead
run:
cargo add solana-program@"=1.17.17"
This will ensure your crate uses only 1.17.17
and nothing else. If you
experience compatibility issues with Solana dependencies, check out the
Solana Stack Exchange
Open your Cargo.toml
file and add these required Rust library configuration
settings, updating your project name as appropriate:
[lib]
name = "hello_world"
crate-type = ["cdylib", "lib"]
Create your first Solana program #
El código para tu programa en Solana basado en Rust vivirá en tu archivo
src/lib.rs
. Dentro de src/lib.rs
podrás importar tus crates de Rust y
definir tu lógica. Open your src/lib.rs
file in your favorite editor.
At the top of lib.rs
, import the solana-program
crate and bring our needed
items into the local namespace:
use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
pubkey::Pubkey,
msg,
};
Cada programa en Solana debe definir un entrypoint
que indica el tiempo de
corrida a la cadena de bloques donde empezará a ejecutar el código. Your
program's
entrypoint
should provide a public function named process_instruction
:
// declara y exporta el punto de entrada del programa
entrypoint!(process_instruction);
// implementación del punto de entrada del programa
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8]
) -> ProgramResult {
// registrar un mensaje en la cadena de bloques
msg!("¡Hola, mundo!");
// sale del programa
Ok(())
}
Cada programa debe devolver Ok
result enum con un valor de ()
. Esto
indica a Solana que su programa se ha ejecutado correctamente y sin errores.
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 Solana Rust program by running in
the root of your project (i.e. the directory with your Cargo.toml
file):
cargo build-bpf
After each time you build your Solana 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. cargo build-bpf
installs the
toolchain from the currently installed solana CLI tools. You may need to
upgrade those tools if you encounter any version incompatibilities.
Deploy your Solana program #
Using the Solana CLI, you can deploy your program to your currently selected cluster:
solana program deploy ./target/deploy/hello_world.so
Once your Solana program has been deployed (and the transaction finalized), the above command will output your program's public address (aka its "program id").
# example output
Program Id: EFH95fWg49vkFNbAdw9vy75tM7sWZ2hQbTTUmuACGip3
¡Felicidades! #
You have successfully setup, built, and deployed a Solana program using the Rust language.
Check your Solana wallet's balance again after you deployed. See how much SOL it cost to deploy your simple program?
Siguientes pasos #
Consulte los siguientes enlaces para obtener más información sobre la escritura de programas en Solana basados en Rust: