In Sway, tuples are a composite data type used to encapsulate multiple values. They can contain elements of different types and can be nested within each other.
_
) to ignore it.In the provided code, we define a smart contract named MyContract
with a function called test_func
that returns a tuple containing an u64
type and another tuple, which includes a str
type and a bool
type.
tuple
with three elements and update its first element.tuple
and assign it to the variable x
.nested
containing a number and another tuple with a string and a boolean value.nested
to extract its internal string s
.nested
again, this time ignoring the first element and the boolean value of the tuple.nested
.The test_func
function of this smart contract ultimately returns a nested tuple. This simple example is intended to show you how to use tuples in Sway to create, read, and update data, as well as how to use nested tuples and destructuring. We hope this tutorial helps you better understand tuples in Sway.
contract; // Enums // - Basics // - Enums of structs // - Enum of enums enum Color { Red: (), Blue: (), Green: (), } // Enums of structs struct Point { x: u64, y: u64, } enum Shape { Circle: (Point, u64), Triangle: [Point; 3], } // Enum of enums enum Error { Auth: AuthError, Transfer: TransferError, } enum AuthError { NotOwner: (), NotApproved: (), } enum TransferError { TransferToZeroAddress: (), InsufficientBalance: (), } abi MyContract { fn test_func() -> Error; } impl MyContract for Contract { fn test_func() -> Error { let color = Color::Blue; let circle = Shape::Circle((Point { x: 0, y: 0 }, 1)); let triangle = Shape::Triangle([ Point { x: 0, y: 0 }, Point { x: 1, y: 1 }, Point { x: 2, y: 0 }, ]); let error = Error::Auth(AuthError::NotOwner); error } }