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:
Step 2: Edit Cargo.toml
Cargo.toml
Open the Cargo.toml
file and add the necessary dependencies and features:
Step 3: Edit Contract Code
Open the src/lib.rs
file and replace the content with the following code:
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 likeString
andVec
.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.
Last updated