Tin

Examples · learn/08-collections · 139 of 339

maps

examples/learn/08-collections/maps.tintin run examples/learn/08-collections/maps.tin
fn main() {
  var stock: Map<string, int> = newMap()
  stock.set("apple", 3)
  stock.set("pear", 0)
  stock.set("apple", 5)                  // set again: replaces the value
  print(stock.get("apple"))              // get returns an Option
  print(stock.get("kiwi"))
  print(stock.has("pear"))
  print(stock.size())
  stock["kiwi"] = 7                      // m[k] = v is the same as set
  stock["kiwi"] += 1
  print(stock["kiwi"])                   // m[k] reads the value; it panics if k is missing
  for fruit, n in stock {                // keys keep the order they were added in
    print("{fruit}: {n}")
  }
}