FromStr Trait
How to parse strings into custom Structs idiomatically using the FromStr trait.
This note is still a seedling.
I keep these in the open and tend them as I learn, so this page grows over time rather than arriving finished. Expect rough edges.
:: ON THIS PAGE 3
The FromStr trait lets you define how to parse a string into your custom type. Once implemented, you get .parse() for free.
The Trait
Example: Parsing a Color
use FromStr;
Usage
Once FromStr is implemented, you can use .parse() with turbofish syntax:
let color: Color = "255, 128, 0".parse.unwrap;
// or
let color = "255, 128, 0"..unwrap;
This is the same mechanism that makes "42".parse::<i32>() work — the standard library implements FromStr for all the primitive types.
> finding connected notes…
> 4 notes share a tag
resp-protocol.md Speaking Redis: Implementing the RESP Protocol key-expiration.md Thread-Safe Storage & Lazy Expiration cpu.md From Monolithic Match to Modular Dispatch e0597.md E0597 > tracing incoming references…
> 2 links found
day-01.md Day 1: Secret Entrance day-02.md Day 2: Gift Shop