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/lib.rs

1#![no_main]
2#![no_std]
3extern crate alloc;
4
5#[global_allocator]
6static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT;
7
8use alloc::vec::Vec;
9
10use stylus_sdk::{console, stylus_proc::entrypoint, ArbResult};
11
12#[panic_handler]
13fn panic(_info: &core::panic::PanicInfo) -> ! {
14    loop {}
15}
16
17#[entrypoint]
18fn user_main(_input: Vec<u8>) -> ArbResult {
19    // Will print 'Stylus says: Hello Stylus!' on your local dev node
20    // Be sure to add "debug" feature flag to your Cargo.toml file as
21    // shown below.
22    console!("Hello Stylus!");
23
24    Ok(Vec::new())
25}
1#![no_main]
2#![no_std]
3extern crate alloc;
4
5#[global_allocator]
6static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT;
7
8use alloc::vec::Vec;
9
10use stylus_sdk::{console, stylus_proc::entrypoint, ArbResult};
11
12#[panic_handler]
13fn panic(_info: &core::panic::PanicInfo) -> ! {
14    loop {}
15}
16
17#[entrypoint]
18fn user_main(_input: Vec<u8>) -> ArbResult {
19    // Will print 'Stylus says: Hello Stylus!' on your local dev node
20    // Be sure to add "debug" feature flag to your Cargo.toml file as
21    // shown below.
22    console!("Hello Stylus!");
23
24    Ok(Vec::new())
25}

Cargo.toml

1[package]
2name = "bytes_in_bytes_out"
3version = "0.1.0"
4edition = "2021"
5
6[dependencies]
7stylus-sdk = "0.5.1"
8mini-alloc = "0.4.2"
9
10[features]
11export-abi = ["stylus-sdk/export-abi"]
12debug = ["stylus-sdk/debug"]
13
14[lib]
15crate-type = ["lib", "cdylib"]
16
17[profile.release]
18codegen-units = 1
19strip = true
20lto = true
21panic = "abort"
22opt-level = "s"
23
24[workspace]
1[package]
2name = "bytes_in_bytes_out"
3version = "0.1.0"
4edition = "2021"
5
6[dependencies]
7stylus-sdk = "0.5.1"
8mini-alloc = "0.4.2"
9
10[features]
11export-abi = ["stylus-sdk/export-abi"]
12debug = ["stylus-sdk/debug"]
13
14[lib]
15crate-type = ["lib", "cdylib"]
16
17[profile.release]
18codegen-units = 1
19strip = true
20lto = true
21panic = "abort"
22opt-level = "s"
23
24[workspace]