You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
leta=Person::default(); //Initialization with default values
100
+
leta=Person::default(); //Instantiation with default values
96
101
97
102
assert_eq!(a.name, ""); // String default value ""
98
103
assert_eq!(a.age, 0.0); // f32 default value 0.0
99
104
}
100
-
101
-
// 💡 #[derive(Default)] attribute automatically generates a default implementation for the Default trait for a struct or enum.
102
105
```
103
106
107
+
💡 In Rust, the `#[derive()]` attribute is used to automatically generate an implementation of certain traits for a custom data structure (struct and enum), instead of you writing them by hand. The [`std::default::Default`](https://doc.rust-lang.org/std/default/trait.Default.html) trait allows us to create a new instance of a type with the `Type::default()` method.
108
+
104
109
💯 5. We can also use a constructor function inside an `impl` block to initialize a struct.
105
110
106
111
### Destructuring
@@ -124,42 +129,14 @@ fn main() {
124
129
company_name:"Apple".to_string(),
125
130
};
126
131
127
-
letPerson {name:a, company_name:b} =steve; // 1. Destructuring elements to a and b
132
+
letPerson {name:a, company_name:b} =steve; // 1. Destructuring fields' values to a and b
128
133
println!("{a} {b}"); // Steve Jobs Apple
129
134
130
-
letPerson {company_name:c, .. } =get_steve(); // 2. Destructuring only selected elements + a struct returned from a function
135
+
letPerson {company_name:c, .. } =get_steve(); // 2. Destructuring only selected fields' values; directly from the function call
131
136
println!("{c}"); // Apple
132
137
}
133
138
134
-
// 💯 let Person {name: ref a, company_name: ref b} = steve; // add ref keyword, to pass an element as a reference
135
-
```
136
-
137
-
### Debug Printing and Pretty Debug Printing
138
-
139
-
> 💡 `#[derive(Debug)]` attribute is used to automatically generate an implementation of the `Debug trait` for a custom data structure, such as a `struct` or an `enum`.
140
-
141
-
```rust
142
-
#![allow(unused)] // 💡 skip unused warnings in crate/ module level
143
-
144
-
#[derive(Debug)]
145
-
structPerson {
146
-
name:String,
147
-
company_name:String,
148
-
}
149
-
150
-
fnmain() {
151
-
let (name, company_name) = ("Steve Jobs".to_string(), "Apple".to_string());
152
-
leta=Person { name, company_name };
153
-
154
-
println!("{a:?}"); // 💡 Debug printing with :? format specifier
155
-
// Person { name: "Steve Jobs", company_name: "Apple" }
156
-
157
-
println!("{a:#?}"); // 💡 Pretty-debug printing with :#? format specifier
158
-
// Person {
159
-
// name: "Steve Jobs",
160
-
// company_name: "Apple",
161
-
// }
162
-
}
139
+
// 💯 let Person {name: ref a, company_name: ref b} = steve; // add ref keyword, to pass a field's value as a reference
163
140
```
164
141
165
142
## Tuple Structs
@@ -176,7 +153,7 @@ struct Color(u8, u8, u8);
176
153
structDepartment(String);
177
154
```
178
155
179
-
### Initialization & Accessing Elements
156
+
### Instantiation & Accessing Elements
180
157
181
158
```rust
182
159
structColor(u8, u8, u8);
@@ -206,43 +183,20 @@ fn get_department() -> Department {
206
183
fnmain() {
207
184
letwhite=Color(255, 255, 255);
208
185
209
-
letColor(red, green, blue) =white; // 💡 let Color(red, blue, .. ) = white; // Destructuring only selected elements
186
+
letColor(red, green, blue) =white; // 💡 let Color(red, blue, .. ) = white; // Destructuring only selected field's value
- It defines a new type, but it resembles an empty tuple, \(\)
196
+
- It defines a new type, but it resembles an empty tuple, `()`
241
197
- This is rarely useful on its own. But in combination with other features (such as generics), it can become useful.
242
198
243
-
> [📖](https://doc.rust-lang.org/book/first-edition/structs.html) ex: A library may ask you to create a structure that implements a certain trait to handle events. If you don’t have any data you need to store in the structure, you can create a unit-like struct.
244
-
245
-
### Definition & Initialization
199
+
### Definition & Instantiation
246
200
247
201
```rust
248
202
structElectron;
@@ -252,15 +206,44 @@ fn main() {
252
206
}
253
207
```
254
208
255
-
### Debug Printing and Pretty Debug Printing
209
+
> [📖](https://doc.rust-lang.org/book/first-edition/structs.html) ex: A library may ask you to create a structure that implements a certain trait to handle events. If you don’t have any data you need to store in the structure, you can create a unit-like struct.
210
+
211
+
## Debug Printing and Pretty Debug Printing
212
+
213
+
In Rust, the `#[derive()]` attribute is used to automatically generate an implementation of certain traits for a custom data structure (struct and enum), instead of you writing them by hand. The [`std::fmt::Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html) trait allows us to format a value with `{:?}` or `{:#?}` in `println!` and similar macros.
256
214
257
215
```rust
216
+
#![allow(unused)] // 💡 skip unused warnings, as we don't read fields in the structs
217
+
258
218
#[derive(Debug)]
259
219
structElectron;
260
220
221
+
#[derive(Debug)]
222
+
structDepartment(String);
223
+
224
+
#[derive(Debug)]
225
+
structPerson {
226
+
name:String,
227
+
company_name:String,
228
+
}
229
+
261
230
fnmain() {
262
231
leta=Electron;
263
-
println!("{a:?}"); // Electron
264
-
println!("{a:#?}"); // Electron
232
+
println!("{a:?}"); // Electron // 💡{a:#?} prints the same
0 commit comments