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.

We will use the following Point structure to illustrate various aspects of generic methods:

24.1.1Constructor (T as argument)

The constructor of our generic struct takes some T arguments and copy or move them into the fields, according to their type:

24.1.2Returning a T

To return a field of type T from a method, we usually return a reference on T. It is always efficient whatever T is. The caller will decide if he/she uses the reference on T as-is to only access the value, or if he/she wants to copy or clone it:

Here is an example in which we only use the returned value:

(1.5, 3.2)

In the following code, however we copy the values into some variables:

(1.5, 3.2)
()