Who owns what#
There is no garbage collector in Manuscript and no lifetime annotation to write. Instead every value has one owner, and the compiler tracks it. This chapter shows what that feels like with a cart of items.
cart.ms
type Cart {
owner String
items List[String]
}
fn summary(cart Cart) String {
return f"{cart.owner} has {cart.items.length()} items"
}
fn add_gift(var cart Cart) Void {
cart.items.append("gift card")
}
fn archive(move cart Cart, var history List[Cart]) Void {
history.append(cart)
}
fn main() Void {
var cart = Cart("Sam", ["pen", "ink"])
print(summary(cart))
add_gift(cart)
print(summary(cart))
var history = List[Cart]()
archive(cart.clone(), history)
cart.items.append("notebook")
print(f"{summary(cart)}; archived: {history[0].items.length()}")
}text
Sam has 2 items
Sam has 3 items
Sam has 4 items; archived: 3What to notice#
summary(cart)only reads the cart. It borrows it, andcartis still yours afterwards.add_gift(cart)may change the cart, becauseadd_giftdeclares the parametervar. The call needs no marker; the signature (andmshover) shows which functions change their arguments, as withmove.archivestores the cart inhistory. Storing moves the value, so the call passescart.clone()and keeps the original..clone()is the only way to copy. Nothing copies a list, a type or a string behind your back, so scanning a change for.clone()andvardeclarations shows you where data is duplicated or changed.
What the compiler says#
Leave out the .clone():
Manuscript
archive(cart, history)
print(cart.owner)The compiler refuses, and its message says how to repair it:
text
cart.ms:11:13: type: `cart` is moved here but used again at 12:11; a value has one owner, so move it only at its last use, or pass `cart.clone()` if the original is still neededTwo repairs fit: clone, as above, or move the value at its last use and stop using the old name.
Why this is worth it#
Memory is freed when the owner goes out of scope, so there is no collector pause and no reference counting on ordinary data. Numbers, booleans, and enums without payloads copy freely, so loops and arithmetic never trip over ownership; move rules apply only to values that own heap data.
Next: Cleaning up . Exact rules: Mutation and references .