Values & References10 / 16
Up next

Defensive copy

import scala.collection.mutable.ArrayBuffer

@main def main(): Unit =
  val source = ArrayBuffer(1, 2, 3)
  // toList copies the contents into an immutable List
  val snapshot = source.toList
  source += 4
  // the snapshot is unaffected by later mutation
  println(s"$snapshot ${source.toList}")
What you're solving

snapshot into an immutable value