In Sway, structs are important tools for encapsulating data, allowing you to create complex data types and easily read and update this data within smart contracts.
.
).In the provided code, we define a smart contract named MyContract
with a function called test_func
that returns an instance of the Line
struct.
Point
struct p0
and update its x
field value.Point
struct p1
.Line
named line
, which contains two instances of the Point
struct.line
, extracting its fields into separate variables x0
, y0
, x1
, and y1
.The test_func
function of this smart contract ultimately returns an instance of the Line
struct. This simple example is intended to show you how to use structs in Sway to create, read, and update data, as well as how to use shorthand notation and struct destructuring. We hope this tutorial helps you better understand structs in Sway.
contract; // Tuples // - Create, read, update // - Nested // - Destructure and "_" abi MyContract { fn test_func() -> (u64, (str, bool)); } impl MyContract for Contract { fn test_func() -> (u64, (str, bool)) { let mut tuple: (u64, bool, u64) = (1, false, 2); tuple.0 = 123; let x = tuple.0; let nested = (1, ("Fuel", false)); let s = nested.1.0; let (n, (s, b)) = nested; // Skip variables for 0 and 1.1 let (_, (s, _)) = nested; nested } }