File tree Expand file tree Collapse file tree 3 files changed +785
-0
lines changed Expand file tree Collapse file tree 3 files changed +785
-0
lines changed Original file line number Diff line number Diff line change
1
+ # import " ../src/block_by_block.typ" : *
2
+
3
+ # set page (
4
+ margin : (
5
+ top : 30pt ,
6
+ bottom : 0pt ,
7
+ left : 50pt ,
8
+ right : 50pt ,
9
+ ),
10
+ // Choose your block color here
11
+ background : [# page_border (blue )],
12
+ )
13
+ // Do not change the font size.
14
+ # set text (size : 13pt )
15
+ # logo (" enums" )
16
+ # block_header (7 , [Rust enums are great!])\
17
+ # what
18
+ \ Everyone knows what enums are — but knowing when to use them isn’t always obvious.
19
+ \ Here’s the rule of thumb: if your type can only take on a few known values, you should enum it.\
20
+ # why
21
+ \ Enums make your code self-documenting, more readable, and give the compiler a chance to catch your bugs before your teammate does.\
22
+ # how
23
+ # bad_code [```rust
24
+ struct LoggingConfig {
25
+ enabled: bool,
26
+ // If not enabled, we don't know the level.
27
+ level: Option<LogLevel>,
28
+ }
29
+ impl LoggingConfig {
30
+ fn log(&self, message: &str) {
31
+ if self.enabled {
32
+ // Why taking the risk of panicking here?
33
+ let level = self.level.unwrap();
34
+ log(level, message);
35
+ }
36
+ }
37
+ }
38
+ ``` ]
39
+ Let's just enum it:
40
+ # good_code [```rust
41
+ enum LoggingConfig {
42
+ Disabled,
43
+ Enabled(LogLevel),
44
+ }
45
+ impl LoggingConfig {
46
+ fn log(&self, message: &str) {
47
+ if let Self::Enabled(level) = self { // No risk of panicking.
48
+ log(level, message);
49
+ }
50
+ }
51
+ }
52
+ ``` ]
53
+ # tip [
54
+ Planning to (de)serialize? Reach for a struct, not an enum.
55
+ Enums tend to confuse other languages.
56
+ ]
57
+ # v (- 20pt )
58
+ # slack
You can’t perform that action at this time.
0 commit comments