This guide walks you through the fundamentals of writing smart contracts on Soroban using the Rust SDK.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/stellar/rs-soroban-sdk/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
Before you begin, ensure you have:- Rust toolchain installed
- Basic understanding of Rust programming
- Soroban SDK added to your
Cargo.toml
Basic Contract Structure
Every Soroban contract follows a standard structure using three key macros:Define the contract struct
Use the
#[contract] macro to mark your contract struct:The contract struct doesn’t need any fields. It serves as a namespace for your contract functions.
Implement contract functions
Use the All
#[contractimpl] macro to implement your contract’s public functions:pub functions in a #[contractimpl] block become part of your contract’s public interface.Working with Storage
Soroban provides three types of storage, each with different persistence characteristics:Persistent Storage
For data that needs to persist long-term:Temporary Storage
For data that only needs to exist for a short time:Instance Storage
For data tied to the contract instance lifetime:Using Constructors
You can initialize contract state using the__constructor function:
The constructor is called once when the contract is deployed and must receive all required arguments.
Environment Parameter
TheEnv type provides access to the contract’s execution environment:
Supported Types
Soroban contracts support various types:Primitive Types
u32,i32,u64,i64,u128,i128bool()(unit type)
SDK Types
Address- Contract and account addressesSymbol- Short strings (up to 32 characters)String- Arbitrary length stringsBytes,BytesN- Binary dataVec- Dynamic arraysMap- Key-value mappings
Custom Types
Use#[contracttype] to define custom types (see Custom Types guide).
Complete Example
Here’s a complete contract that demonstrates key concepts:Next Steps
Testing Contracts
Learn how to write tests for your contracts
Custom Types
Define custom data structures
Error Handling
Handle errors gracefully in your contracts
Events
Publish events from your contracts