how to

default-parameters

Mar 27, 2024
langsrust
1 Minutes
123 Words

ref: https://www.thecodedmessage.com/posts/default-params/

Sol 1

1
impl Default for WindowConfig {
2
fn default() -> Self {
3
Self {
4
width: 100,
5
height: 100,
6
visibility: WindowVisibility::Visible,
7
window_style: WindowStyle::Standard,
8
z_position: -1,
9
autoclose: AutoclosePolicy::Disable,
10
}
11
}
12
}
13
14
let handle = create_window(WindowConfig {
15
width: 500,
4 collapsed lines
16
z_position: 2,
17
autoclose: AutoclosePolicy::Enable,
18
..Default::default()
19
});

Sol 2..

1
impl WindowBuilder {
2
fn height(mut self, height: u32) -> Self {
3
self.height = height;
4
self
5
}
6
7
// ...
8
}
9
10
impl WindowBuilder {
11
fn height(self, height: u32) -> Self {
12
Self {
13
height,
14
..self
15
}
29 collapsed lines
16
}
17
18
// ...
19
}
20
21
impl WindowBuilder {
22
fn autoclose_enable(mut self) -> Self {
23
self.autoclose = AutoclosePolicy::Enable;
24
self
25
}
26
27
fn autoclose_disable(mut self) -> Self {
28
self.autoclose = AutoclosePolicy::Disable;
29
self
30
}
31
}
32
33
impl WindowBuilder {
34
fn build(self) {
35
window_create(self)
36
}
37
}
38
39
40
let handle = WindowBuilder::new()
41
.width(500)
42
.z_position(2)
43
.autoclose_enable()
44
.build();
Article title:default-parameters
Article author:Julyfun
Release time:Mar 27, 2024
Copyright 2025
Sitemap