Using Rust
Recording some errors and refactoring encountered while writing Rust. Option When to use .as_ref()? When you want to convert an Option<T> to an Option<&T> without consuming the original Option. ...
Recording some errors and refactoring encountered while writing Rust. Option When to use .as_ref()? When you want to convert an Option<T> to an Option<&T> without consuming the original Option. ...
55: Mixing up concurrency and parallelism Concurrency enables parallelism. Concurrency provides a structure to solve a problem with parts that may be parallelized. Quote “Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.” ...
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. ...
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. ...
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. ...
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: ...
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. ...
17 Creating confusion with octal literals In Go, an integer literal starting with 0 is considered an octal integer (base 8). Note the integer literal representations in Golang: Binary - Uses a 0b or 0B prefix (for example, 0b100 is equal to 4 in base 10) Octal - Uses a 0 or 0o prefix (for example, 0o10 is equal 8 in base 10) Hexadecimal - Uses an 0x or 0X prefix (for example, 0xF is equal to 15 in base 10) Imaginary - Uses an i suffix (for example, 3i) 20 Not understanding slice length and capacity In Go, a slice is backed by an array. That means the slice’s data is stored contiguously in an array data structure. A slice also handles the logic of adding an element if the backing array is full or shrinking the backing array if it’s almost empty. ...