100 Exercises to Learn Rust Note

4 Traits To invoke a trait method, two things must be true: The type must implement the trait. The trait must be in scope. To satisfy the latter, you may have to add a use statement for the trait: use crate::MaybeZero; This is not necessary if: The trait is defined in the same module where the invocation occurs. The trait is defined in the standard library’s prelude. The prelude is a set of traits and types that are automatically imported into every Rust program. It’s as if use std::prelude::*; was added at the beginning of every Rust module. Orphan Rule When a type is defined in another crate (e.g. u32, from Rust’s standard library), you can’t directly define new methods for it. ...

March 13, 2025 · Last updated on April 23, 2025 · 5 min · KKKZOZ

100 Mistakes in Golang: Chapter 7

48: Panicking Panicking in Go should be used sparingly. We have seen two prominent cases, one to signal a programmer error and another where our application fails to create a mandatory dependency. 49: Ignoring when to wrap an error In general, the two main use cases for error wrapping are the following: Adding additional context to an error Marking an error as a specific error if err != nil { return fmt.Errorf("bar failed: %w", err) } This code wraps the source error to add additional context without having to create another error type. ...

March 7, 2025 · Last updated on April 5, 2025 · 2 min · KKKZOZ

100 Mistakes in Golang: Chapter 6

42: Not knowing which type of receiver to use In Go, we can attach either a value or a pointer receiver to a method. With a value receiver, Go makes a copy of the value and passes it to the method. Any changes to the object remain local to the method. The original object remains unchanged. On the other hand, with a pointer receiver, Go passes the address of an object to the method. Intrinsically, it remains a copy, but we only copy a pointer, not the object itself (passing by reference doesn’t exist in Go). Any modifications to the receiver are done on the original object. ...

March 4, 2025 · Last updated on April 5, 2025 · 5 min · KKKZOZ

100 Mistakes in Golang: Chapter 5

36: Not understanding the concept of a rune The len built-in function applied on a string doesn’t return the number of characters; it returns the number of bytes. What if we want to get the number of runes in a string, not the number of bytes? How we can do this depends on the encoding. In the previous example, because we assigned a string literal to s, we can use the unicode/utf8 package: ...

March 3, 2025 · Last updated on April 5, 2025 · 3 min · KKKZOZ

100 Mistakes in Golang: Chapter 2

2 Unnecessary nested code 能够提前返回的特殊判定就提前返回 if xxx { // .... return err }else{ // do X } 总是能够写为 if xxx{ return err } // do X Make expected execution flow clear 5 Interface pollution Quote The bigger the interface, the weaker the abstraction. ...

February 20, 2025 · Last updated on April 5, 2025 · 7 min · KKKZOZ