The static methods are mainly used to constructor and return an
instance of the struct. By convention the default constructor is
called new(), but we can define others.
In the following example we define a Window struct that represents an
application window:
struct Window {
x: u16,
y: u16,
width: u16,
height: u16,
}The new() constructor takes as arguments all values required for
fields, and returns a Window instance denoted by the Self type:
impl Window {
fn new(x: u16, y:u16, width: u16, height: u16) -> Self {
Self {
x,
y,
width,
height,
}
}
}Here is an example of usage of the constructor:
let wnd = Window::new(10, 20, 200, 80);
(wnd.x, wnd.y, wnd.width, wnd.height)(10, 20, 200, 80)