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 contain an ID, price, the owner's identity, a string representing a URL or identifier for off-chain data about the item (such as its description and photos), and a "total bought" counter to track the overall 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, making the value always positive. u64 is the default type used for numbers in Sway. To use other number types, such as u256 Icon Link or signed integers Icon Link, you must import them from a library.

In JavaScript, there are two types of integers: number and BigInt. The primary difference between these types is that BigInt can store much larger values. Similarly, each numeric type in Sway has its maximum value that can be stored.

String: a string created from the string type in the standard library. If you want to use the str keyword, you must define a fixed length, and it 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. In Sway, a contract and an EOA (Externally Owned Account) are distinctly differentiated. Both are typesafe wrappers for b256.

Was this page helpful?

Icon ListDetailsOn this page