Jonah SC HW 2 - TheEvergreenStateCollege/upper-division-cs-23-24 GitHub Wiki

Reading and Quizzes

Chapter 5

The .. operator copies field values from one struct into another being initialized, excluding whatever fields are already assigned values. Because these are copied and not borrowed the second increment of a.x has no effect on b.x.

p.y and p.x refer to different objects, so each can be modified using multiple mutable references.

On line 13, rect1 is moved when passed to the area() function, so its fields cannot be accessed on the next line which prints their values.

The debug trait is used to display the value of structs to developers (just prints the values of its fields as a list). The display trait is implemented for specific structs to prettify their printing for an end-user.

When calling a method on a mutable reference Rust automatically calls the method with an immutable reference to self.

The Point object is consumed by the method when calling incr_v1() as the self parameter is not declared as a reference in the function's definition. incr_v2() is idiomatic Rust as it does not consume the object the method is called on.

The .0 syntax accesses the first field of a struct.

The get_x() method implemented for Point borrows the Point object on which it's called, meaning that its fields cannot be dereferenced until the object is returned from the borrow.

Chapter 6

Chapter 7