Arbitrum Stylus logo

Stylus by Example

Hello World

Using the console! macro from the stylus_sdk allows you to print output to the terminal for debugging purposes. To view the output, you'll need to run a local Stylus dev node as described in the Arbitrum docs and set the debug feature flag as shown in line 7 of the Cargo.toml file below.

The console! macro works similar to the built-in println! macro that comes with Rust.

Examples

1// Out: Stylus says: 'hello there!'
2console!("hello there!");
3// Out: Stylus says: 'format some arguments'
4console!("format {} arguments", "some");
5
6let local_variable = "Stylus";
7// Out: Stylus says: 'Stylus is awesome!'
8console!("{local_variable} is awesome!");
9// Out: Stylus says: 'When will you try out Stylus?'
10console!("When will you try out {}?", local_variable);
1// Out: Stylus says: 'hello there!'
2console!("hello there!");
3// Out: Stylus says: 'format some arguments'
4console!("format {} arguments", "some");
5
6let local_variable = "Stylus";
7// Out: Stylus says: 'Stylus is awesome!'
8console!("{local_variable} is awesome!");
9// Out: Stylus says: 'When will you try out Stylus?'
10console!("When will you try out {}?", local_variable);

src/main.rs

1#![cfg_attr(not(feature = "export-abi"), no_main)]
2
3extern crate alloc;
4
5
6use stylus_sdk::{console, prelude::*, stylus_proc::entrypoint, ArbResult};
7
8#[storage]
9#[entrypoint]
10pub struct Hello;
11
12
13#[public]
14impl Hello {
15    fn user_main(_input: Vec<u8>) -> ArbResult {
16        // Will print 'Stylus says: Hello Stylus!' on your local dev node
17        // Be sure to add "debug" feature flag to your Cargo.toml file as
18        // shown below.
19        console!("Hello Stylus!");
20        Ok(Vec::new())
21    }
22}
1#![cfg_attr(not(feature = "export-abi"), no_main)]
2
3extern crate alloc;
4
5
6use stylus_sdk::{console, prelude::*, stylus_proc::entrypoint, ArbResult};
7
8#[storage]
9#[entrypoint]
10pub struct Hello;
11
12
13#[public]
14impl Hello {
15    fn user_main(_input: Vec<u8>) -> ArbResult {
16        // Will print 'Stylus says: Hello Stylus!' on your local dev node
17        // Be sure to add "debug" feature flag to your Cargo.toml file as
18        // shown below.
19        console!("Hello Stylus!");
20        Ok(Vec::new())
21    }
22}

Cargo.toml

1[package]
2name = "stylus_hello_world"
3version = "0.1.7"
4edition = "2021"
5license = "MIT OR Apache-2.0"
6keywords = ["arbitrum", "ethereum", "stylus", "alloy"]
7
8[dependencies]
9alloy-primitives = "=0.7.6"
10alloy-sol-types = "=0.7.6"
11mini-alloc = "0.4.2"
12stylus-sdk = { version = "0.6.0", features = ["debug"] }
13hex = "0.4.3"
14sha3 = "0.10"
15
16[dev-dependencies]
17tokio = { version = "1.12.0", features = ["full"] }
18ethers = "2.0"
19eyre = "0.6.8"
20
21[features]
22export-abi = ["stylus-sdk/export-abi"]
23
24[lib]
25crate-type = ["lib", "cdylib"]
26
27[profile.release]
28codegen-units = 1
29strip = true
30lto = true
31panic = "abort"
32opt-level = "s"
1[package]
2name = "stylus_hello_world"
3version = "0.1.7"
4edition = "2021"
5license = "MIT OR Apache-2.0"
6keywords = ["arbitrum", "ethereum", "stylus", "alloy"]
7
8[dependencies]
9alloy-primitives = "=0.7.6"
10alloy-sol-types = "=0.7.6"
11mini-alloc = "0.4.2"
12stylus-sdk = { version = "0.6.0", features = ["debug"] }
13hex = "0.4.3"
14sha3 = "0.10"
15
16[dev-dependencies]
17tokio = { version = "1.12.0", features = ["full"] }
18ethers = "2.0"
19eyre = "0.6.8"
20
21[features]
22export-abi = ["stylus-sdk/export-abi"]
23
24[lib]
25crate-type = ["lib", "cdylib"]
26
27[profile.release]
28codegen-units = 1
29strip = true
30lto = true
31panic = "abort"
32opt-level = "s"