Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Once an object has been built, we may modify its data through setter methods. Those methods take a mutable reference to the self instance, and thus can modify the values of the fields. We usually name the setter methods using the set_ prefix.

As an example, let us write a struct representing the weight of a car, with its empty weight and the current volume of petrol:

The constructor accepts only the empty weight, the volume of petrol being set to a default value of 0L:

We implement a builder setter for the volume of petrol:

And also a setter method:

We create an instance of Car:

(800.0, 60.0)

And we modify the petrol volume in this instance using the setter method:

(800.0, 40.0)

see solution

see solution