Version: 1.1.0
Value semantics
Mojo defaults to value semantics. A function receives its own view of the data unless you deliberately pass a reference.
Essentials
- Copying a
Listcopies the elements, so later edits stay local. - Types that should not be copied silently require an explicit transfer or copy.
- Value semantics makes aliasing visible in the signature.
- Remote or huge buffers can still live behind a pointer inside the value.
Example
def fill(mut items: List[Int]):
items.append(1)
def main():
var nums = List[Int]()
fill(nums)
print(len(nums))
Keep in mind
If two names must observe the same mutation, pass mut or read instead of copying.