stylusguide
  • 1. Welcome to Arbitrum Stylus and Rust
    • Brief Introduction to Arbitrum Stylus
    • Importance of Rust in Blockchain Development
    • What You'll Learn in This Tutorial Series
  • 2. Setting Up Your Development Environment
    • Installing Rust
    • Setting Up Arbitrum Stylus
    • Basic Commands
  • 3. Your First Smart Contract: Hello World
    • Introduction to Smart Contracts
    • Writing the Hello World Contract
    • Deploying and Testing Your Contract
  • 4. Understanding Smart Contract Storage: Simple Storage
    • Basics of Contract Storage
    • Writing the Simple Storage Contract
    • Interacting with Storage Variables
  • 5. Creating Your Own Token: Token Contract
    • Introduction to Tokens and Standards
    • Writing the Token Contract
    • Deploying and Testing Your Token
  • 6. Additional Resources and Next Steps
Powered by GitBook
On this page
  • Project Setup
  • Step 1: Create a New Project
  • Step 2: Edit Cargo.toml
  • Step 3: Edit Contract Code
  1. 3. Your First Smart Contract: Hello World

Writing the Hello World Contract

Project Setup

Step 1: Create a New Project

Create a new project using cargo-stylus and navigate into the project directory:

cargo stylus new hello_world
cd hello_world

Step 2: Edit Cargo.toml

Open the Cargo.toml file and add the necessary dependencies and features:

[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"

[dependencies]
stylus-sdk = { version = "0.5.2", features = ["debug"] }
wee_alloc = "0.4.5"

[features]
export-abi = ["stylus-sdk/export-abi"]

[profile.release]
codegen-units = 1
strip = true
lto = true
panic = "abort"
opt-level = "s"

[workspace]

Step 3: Edit Contract Code

Open the src/lib.rs file and replace the content with the following code:

// Disable Rust standard library to keep the contract small.
#![no_main]
#![no_std]

// Import the necessary crates and modules.
extern crate alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

use alloc::string::String;
use alloc::vec::Vec;
use stylus_sdk::{console, stylus_proc::entrypoint, ArbResult};

// Define the entrypoint for the contract.
#[entrypoint]
fn user_main(_input: Vec<u8>) -> ArbResult {
    // Print a "Hello, world!" message to the console.
    console!("Hello, world!");
    Ok(Vec::new())
}

Explanation

  • No Standard Library: #![no_main] and #![no_std] are used to disable the Rust standard library, which keeps the contract's WASM size small.

  • Allocator: wee_alloc is a small allocator designed for WASM environments, reducing the contract's footprint.

  • Imports: alloc provides heap-allocated types like String and Vec. stylus_sdk includes necessary macros and functions for Stylus development.

  • Entry Point: #[entrypoint] defines the starting point of the contract. console! macro prints "Hello, world!" to the console for debugging purposes.

PreviousIntroduction to Smart ContractsNextDeploying and Testing Your Contract

Last updated 10 months ago