Developing Programs in Rust
Solana programs are primarily developed using the Rust programming language. This page focuses on writing Solana programs in Rust without using the Anchor framework, an approach often referred to as writing "native Rust" programs.
Native Rust development provides developers with direct control over their Solana programs. However, this approach requires more manual setup and boilerplate code compared to using the Anchor framework. This method is recommended for developers who:
- Seek granular control over program logic and optimizations
- Want to learn the underlying concepts before moving to higher-level frameworks
For beginners, we recommend starting with the Anchor framework. See the Anchor section for more information.
Prerequisites
For detailed installation instructions, visit the installation page.
Before you begin, ensure you have the following installed:
- Rust: The programming language for building Solana programs.
- Solana CLI: Command-line tool for Solana development.
Getting Started
The example below covers the basic steps to create your first Solana program written in Rust. We'll create a minimal program that prints "Hello, world!" to the program log.
Create a new Program
First, create a new Rust project using the standard cargo init
command with
the --lib
flag.
Navigate to the project directory. You should see the default src/lib.rs
and
Cargo.toml
files
Next, add the solana-program
dependency. This is the minimum dependency
required to build a Solana program.
Next, add the following snippet to Cargo.toml
. If you don't include this
config, the target/deploy
directory will not be generated when you build the
program.
Your Cargo.toml
file should look like the following:
Next, replace the contents of src/lib.rs
with the following code. This is a
minimal Solana program that prints "Hello, world!" to the program log when the
program is invoked.
The msg!
macro is used in Solana programs to print a message to the program
log.
Build the Program
Next, build the program using the cargo build-sbf
command.
This command generates a target/deploy
directory containing two important
files:
- A
.so
file (e.g.,hello_world.so
): This is the compiled Solana program that will be deployed to the network as a "smart contract". - A keypair file (e.g.,
hello_world-keypair.json
): The public key of this keypair is used as the program ID when deploying the program.
To view the program ID, run the following command in your terminal. This command prints the public key of the keypair at the specified file path:
Example output:
Test the Program
Next, test the program using the solana-program-test
crate. Add the following
dependencies to Cargo.toml
.
Add the following test to src/lib.rs
, below the program code. This is a test
module that invokes the hello world program.
Run the test using the cargo test-sbf
command. The program log will display
"Hello, world!".
Example output:
Deploy the Program
Next, deploy the program. When developing locally, we can use the
solana-test-validator
.
First, configure the Solana CLI to use the local Solana cluster.
Example output:
Open a new terminal and run the solana-test-validators
command to start the
local validator.
While the test validator is running, run the solana program deploy
command in
a separate terminal to deploy the program to the local validator.
Example output:
You can inspect the program ID and transaction signature on
Solana Explorer.
Note that the cluster on Solana Explorer must also be localhost. The "Custom RPC
URL" option on Solana Explorer defaults to http://localhost:8899
.
Invoke the Program
Next, we'll demonstrate how to invoke the program using a Rust client.
First create an examples
directory and a client.rs
file.
Add the following to Cargo.toml
.
Add the solana-client
dependency.
Add the following code to examples/client.rs
. This is a Rust client script
that funds a new keypair to pay for transaction fees and then invokes the hello
world program.
Before running the script, replace the program ID in the code snippet above with the one for your program.
You can get your program ID by running the following command.
Run the client script with the following command.
Example output:
You can inspect the transaction signature on Solana Explorer (local cluster) to see "Hello, world!" in the program log.
Update the Program
Solana programs can be updated by redeploying to the same program ID. Update the
program in src/lib.rs
to print "Hello, Solana!" instead of "Hello, world!".
Test the updated program by running the cargo test-sbf
command.
You should see "Hello, Solana!" in the program log.
Run the cargo build-sbf
command to generate an updated .so
file.
Redeploy the program using the solana program deploy
command.
Run the client code again and inspect the transaction signature on Solana Explorer to see "Hello, Solana!" in the program log.
Close the Program
You can close your Solana program to reclaim the SOL allocated to the account. Closing a program is irreversible, so it should be done with caution.
To close a program, use the solana program close <PROGRAM_ID>
command. For
example:
Example output:
Note that once a program is closed, its program ID cannot be reused. Attempting to deploy a program with a previously closed program ID will result in an error.
If you need to redeploy a program with the same source code after closing a program, you must generate a new program ID. To generate a new keypair for the program, run the following command:
Alternatively, you can delete the existing keypair file (e.g.
./target/deploy/hello_world-keypair.json
) and run cargo build-sbf
again,
which will generate a new keypair file.