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.

50.10.1Rectangle area

The rectangle structure has two fields width and height:

The area method takes a reference on self and does its computing:

Here is a call to area() on an example of instance:

200

50.10.2Rectangle & square

The rectangle structure may be implemented as following, using 32-bit floats:

The default constructor takes both the width and the height to build a new Rect object:

The square() constructor takes only one f32 value as argument. It is used to set both width and height:

Here is an example of building a regular rectangle:

(5.0, 4.5)

The same for a square:

(10.0, 10.0)

50.10.3Postal mail

Here is a possible structure for modelling a postal mail:

We implement the new() constructor and the builder setters for weight and priority:

An example of Mail instance:

The content of the instance:

("RMN 254 rue de Bercy 75577 Paris Cedex 12", "Marcel Dupond 3 rue de Bruxelles 59000 Lille", 100.0, true)

50.10.4Book

We define the Book structure, its constructor and three access functions to access the values of the three fields:

Here is an example of usage of the constructor and the access functions:

("Robinson Crusoe", "Daniel Defoe", 1719)

In both the constructor function and the access functions, the idea is to encapsulate the construction and the access. If later we need to run code during construction or access, it will be easy inserted inside the function.

50.10.5Square area

We define a struct named Square that we can define be either giving the area or the side length. From one value the constructor function computes the other, leaving the original value untouched. This example shows how we can force to run the same computation for each constructed object:

Excepted the representation error, the original value of the area of the following square is untouched, while the re-computed area propagates an error:

a=1.0000499487, a*a=1.0001000166
a=1.0000499487, a*a=1.0000998974 (recomputed)