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
- Assigning
calldatato a state string copies into storage. memoryparameters are mutable scratch copies.string.concatbuilds new memory strings.
Keywords and builtins used here
contractexternalfunctionpublicreturnreturnsstring
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.
Step 1 of 3 in Data & gas, step 14 of 28 in Language basics.