Why is Substring a TextOutputStream?

Slice types are allowed to share storage with a parent value, but are otherwise completely independent values. Appending to a slice is useful when modifying a slice through an inout parameter, which will write the slice back to the parent:

var x = [1, 2, 3, 5]

func insert(after: inout ArraySlice<Int>) {
  after += [4]
}

insert(after: &x[1..2])
print(x)
7 Likes