Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 189bf38

Browse files
structs2.rs added
1 parent 3d18637 commit 189bf38

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Unit struct don't have any fields. It can be useful when you need to implement a trait on some type but don’t have any data that you want to store in the type itself.
2+
3+
/*
4+
5+
struct Unit;
6+
trait SomeTrait {
7+
// ...Some behaviors defined here.
8+
}
9+
10+
// We don't care about what fields are in the Unit, but we care about its behaviors.
11+
// So we use a struct with no fields and implement some behaviors for it
12+
impl SomeTrait for Unit { }
13+
fn main() {
14+
let u = Unit;
15+
do_something_with_unit(u);
16+
17+
println!("Success!");
18+
}
19+
20+
/ Fill the blank to make the code work
21+
fn do_something_with_unit(u: __) { }
22+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /