Primitive Data Types
Learn about the essential data types for Stylus contracts including integers, addresses, and bytes.
Primitive Data Types
The Stylus SDK uses the Alloy library to represent Solidity types as Rust types.
Integers
Unsigned integers (U256, U128, U8, etc.) range from 0 to 2^n - 1:
`` let eight_bit: U8 = U8::from(1); let two_fifty_six_bit: U256 = U256::from(0xff_u64);rust
`
Signed integers (I256, I128, I8, etc.) allow negative values:
` let negative: I8 = I8::unchecked_from(-1); let positive: I256 = I256::unchecked_from(100);rust
`
Address
Ethereum addresses are 20 bytes:
` use alloy_primitives::{address, Address}; let addr = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");rust
`
Boolean
Standard Rust bool type works directly.
Bytes
The Bytes type wraps Vec for Solidity's bytes memory:
` let bytes = Bytes::from(vec![0x01, 0x02, 0x03]);rust
``
Code Example
use alloy_primitives::{U256, U8, I256, Address, address};
use stylus_sdk::alloy_primitives::Bytes;
// Unsigned integers
let small: U8 = U8::from(255);
let large: U256 = U256::from(1_000_000);
// Signed integers
let negative: I256 = I256::unchecked_from(-100);
// Addresses
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
// Bytes
let data = Bytes::from(vec![0xde, 0xad, 0xbe, 0xef]);
// Integer operations
let sum = large + U256::from(1);
let max = U256::MAX;
let zero = U256::ZERO;