Skip to main content

Getting Started with Rust

Safe Password Storage

In a production setup, do not store passwords in the host's environment variables or in the source code. See our backup and security recommendations for production setups.

Install the IOTA SDK

Requirements

The IOTA SDK requires Rust and Cargo. You can find installation instructions in the Rust documentation.

We recommend that you update the Rust compiler to the latest stable version first:

rustup update stable

Dependencies

You must also install cmake, clang, and openssl. You may need to install additional build tools on your system to run the build process successfully using Cargo.

You can install cmake, clang, and openssl with your distro's package manager or download them from their websites. On Debian and Ubuntu, you will also need the build-essential and libudev-dev packages.

Install the SDK Using Cargo

To start using the IOTA SDK in your Rust project, you can include the following dependencies in your Cargo.toml file:

[dependencies]
iota-sdk = { branch = "develop" }

Build the SDK from Source

  1. Download the repository to any directory you choose:

    git clone https://github.com/iotaledger/iota-sdk
  2. Move to the base directory:

    cd iota-sdk/
  3. Build the SDK by running the following:

    cargo build

Usage

Client

To use the Client module, you simply need to create a Client.

use iota_sdk::client::{
Client,
};

let client = Client::builder()
.with_node("https://api.testnet.shimmer.network")? // Insert your node URL here
.finish()
.await?;

let info = client.get_info().await?;
println!("Node Info: {info:?}")

Ok(())

Wallet

To use the Wallet module, you need to create a Wallet:

use iota_sdk::{
client::{
constants::SHIMMER_COIN_TYPE,
secret::{stronghold::StrongholdSecretManager, SecretManager},
},
wallet::{ClientOptions, Result, Wallet},
};
use std::path::PathBuf;

#[tokio::main]
async fn main() -> Result<()> {
// Setup Stronghold secret manager.
let secret_manager = StrongholdSecretManager::builder()
.password("vault.stronghold") // A password to encrypt the stored data.WARNING: Never hardcode passwords in production code.
.build(PathBuf::from("vault.stronghold"))?; // The path to store the account snapshot.

let client_options = ClientOptions::new().with_node("https://api.testnet.shimmer.network")?;// The node to connect to.

// Set up and store the wallet.
let wallet = Wallet::builder()
.with_secret_manager(SecretManager::Stronghold(secret_manager))
.with_client_options(client_options)
.with_coin_type(SHIMMER_COIN_TYPE)
.finish()
.await?;

// Generate a mnemonic and store it in the Stronghold vault.
// INFO: It is best practice to back up the mnemonic somewhere secure.
let mnemonic = wallet.generate_mnemonic()?;
println!("Creating a wallet with mnemonic:\n'{mnemonic}'");
wallet.store_mnemonic(mnemonic).await?;

// Create an account.
let account = wallet
.create_account()
.with_alias("Alice".to_string()) // A name to associate with the created account.
.finish()
.await?;


Ok(())
}

What's next?

How-To Guides

Once you have installed the IOTA SDK, you can start building your application. You can find usage examples in this Wiki's how-to guides.

More Examples

You can use the provided code examples to get acquainted with the IOTA SDK. You can use the following command to run any example:

cargo run --release --all-features --example example_name

* Where `example_name` is the name from
the [Cargo.toml](https://github.com/iotaledger/iota-sdk/tree/develop/sdk/sdk/Cargo.toml) name from the example folder.
For example:

```bash
cargo run --release --all-features --example node_api_core_get_info

You can get a list of the available code examples with the following command:

cargo run --example

API Reference

The IOTA SDK Rust API Reference is in the crate documentation.