Simulating References
Everything on the Sui blockchain is an object. When you develop Move packages for the Sui network, you're typically manipulating or using on-chain objects in some way through functionality available in the Sui API. For most API functions, you provide an object by reference.
References are a key construct when programming in Move and on Sui. Most of the functionality available in the Sui API takes objects by reference.
There are two ways to use an object:
- by value: When you use an object by value, you have full control over that object. You can destroy it (if the functionality is available), wrap it (if it has the
store
ability), or transfer it to an address. - by reference: When you use an object by reference, operations over that object are determined by the logic the module that defines the object provides because you are using a reference to its data rather than having ownership of the object itself. The restrictions of references allow you to develop smart contracts with a high level of security and safety around assets. There are two types of references:
- Mutable reference (
&mut
): You can alter the object (according to the API) but you can't destroy or transfer it. - Immutable reference (
&
): Further restricts the set of operations and the guarantees/invariants over the referenced object. You have read-only access to the object's data.
- Mutable reference (
Programmable transaction blocks (PTBs) do not currently allow the use of object references returned from one of its transaction commands. You can use input objects to the PTB, objects created by the PTB (like MakeMoveVec
), or returned from a transaction command by value as references in subsequent transaction commands. If a transaction command returns a reference, however, you can't use that reference in any call, significantly limiting certain common patterns in Move.