Structs and value semantics
Structs and value semantics
A student record belongs together: a name, a score and operations on that record. A struct defines a new value type containing properties and methods. Assigning a struct produces another logical value, so changing a copy does not change the original record.
What you’ll learn
- Trace a struct copy and a mutating method.
- Distinguish wrapper value semantics from reference-typed members.
Structs and value semantics in the example
Original record
A student record belongs together: a name, a score and operations on that record.
Original recordCopy the value
Properties describe stored data.
Copy the valueChange only the copy
The original score stays 70, while copy becomes 75.
Change only the copy
How structs and value semantics works
Properties describe stored data. Methods are functions attached to the type. A method that changes a struct’s own stored state must be marked mutating, and the instance being changed must be mutable. A simple struct receives a memberwise initializer when its declarations allow it.
Work through structs and value semantics
Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.
Swift · complete program
struct Student {
let name: String
var score: Int
mutating func addBonus() { score += 5 }
}
let original = Student(name: "Diya", score: 70)
var copy = original
copy.addBonus()
print(original.score, copy.score)Expected output
70 75Follow the execution
The original score stays 70, while copy becomes 75. The method changes only the mutable instance on which it was called. This observable independence is value semantics, regardless of how storage is optimized internally.
Try this program in the Swift compiler. Paste the complete source above.
Practice: Structs and value semantics
Your task: Can original.addBonus() compile while original is let?
Swift · complete program
struct Student {
let name: String
var score: Int
mutating func addBonus() { score += 5 }
}
let original = Student(name: "Diya", score: 70)
var copy = original
copy.addBonus()
print(original.score, copy.score)Worked answer
No. The mutating method changes the struct value, and a let-bound struct cannot be mutated. Keep the original constant or declare a mutable instance for an intentional update.
Distinguish value copies from shared members
A struct can contain references to class instances, so copying the struct does not necessarily clone every object reachable from it. Distinguish value semantics of the wrapper from reference semantics of its members.
Check your decision
Check the practical consequence
Distinguish value copies from shared members
No. The mutating method changes the struct value, and a let-bound struct cannot be mutated. Keep the original constant or declare a mutable instance for an intentional update. A struct can contain references to class instances, so copying the struct does not necessarily clone every object reachable from it.
Further reading: official Swift language guide.
Reaching the recap and completing the quizzes are separate milestones.