typestar

Data location in Solidity

storage persists, memory is scratch, calldata is the call.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

// Data location matters: storage persists, memory is scratch,
// calldata is the read-only bytes of the call itself.
contract LabelMaker {
    string public stored;

    function keep(string calldata label) external {
        stored = label;  // calldata copies into storage
    }

    function shout(string memory label)
        public pure returns (string memory)
    {
        return string.concat(label, "!");
    }

    function shoutStored() external view returns (string memory) {
        return shout(stored);  // storage copies into memory
    }
}

How it works

  1. Assigning calldata to a state string copies into storage.
  2. memory parameters are mutable scratch copies.
  3. string.concat builds new memory strings.

Keywords and builtins used here

The run, in numbers

Lines
22
Characters to type
551
Tokens
76
Three-star pace
55 tpm

At the three-star pace of 55 tokens a minute, this run takes about 83 seconds.

Type this snippet

Step 1 of 3 in Data & gas, step 14 of 28 in Language basics.

← Previous Next →