Rotate the rehearsal queue
Before you start this intermediate track
Functions and collections. Work with structs, mutable bindings, and optionals.
Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.
Understand it, then try it.
A rehearsal queue uses an array to preserve performer order. removeFirst removes and returns the front element, and append moves that performer to the back. The first property is optional because an array could be empty. The fallback gives the display a sensible label if no performer is available. This example starts with a nonempty array. removeFirst() requires at least one element; the later fallback for first does not make removal safe on an empty array. Check isEmpty before removing from an array that might be empty.
var performers = ["Oboe", "Cello", "Viola"]let finished = performers.removeFirst()performers.append(finished)let next = performers.first ?? "No performer"print(next)print(performers.count)Run each mission independently as a Swift 5.9 or later script or playground. Top-level statements execute directly; these examples need only the Swift standard library and no Apple UI frameworks.
Track the values line by line. Predict the result before running this example in your own editor.