Icon LinkDefining an Item Struct

Struct is short for structure, which is a data structure similar to an object in JavaScript. You define a struct with the struct keyword in Sway and define the fields of a struct inside curly brackets.

The core of our program is the ability to list, sell, and get Items.

Let's define the Item type as shown below:

struct Item {
	id: u64,
	price: u64,
	owner: Identity,
	metadata: String,
	total_bought: u64,
}

The item struct will hold an ID, price, the owner's identity, a string for a URL or identifier where off-chain data about the item is stored (such as the description and photos), and a total bought counter to keep track of the total number of purchases.

Icon LinkTypes

The Item struct uses three types: u64, String, and Identity.

u64: a 64-bit unsigned integer

In Sway, there are four native types of numbers:

  • u8: an 8-bit unsigned integer
  • u16 a 16-bit unsigned integer
  • u32 a 32-bit unsigned integer
  • u64: a 64-bit unsigned integer

An unsigned integer means there is no + or - sign, so the value is always positive. u64 is the default type used for numbers in Sway. To use other number types, for example u256 Icon Link or signed integers Icon Link, you must import them from a library.

In JavaScript, there are two types of integers: a number and a BigInt. The main difference between these types is that BigInt can store a much larger value. Similarly, each number type for Sway has different values for the largest number that can be stored.

String: a string with created from the string in the standard library. If you want to use the str keyword you must define a fixed length and cannot be changed.

Identity: an enum type that represents either a user's Address or a ContractId. We already imported this type from the standard library earlier. To a contract or and EOA (Externally owned account) which are two clearly differentiated in Sway. Which are both just typesafe wrapped b256.

Was this page helpful?

Icon ListDetailsOn this page