Edvard Chen
2 min readNov 30, 2022

--

Rust narrows down our everyday API set

Aside from being a language that emphasizes memory security extremely, Rust is surprisingly more expressive than people expected.

I realized it when I learned the str.parse method. See the following example

let ip: IpAddr = "127.0.0.1".parse().unwrap();
let path: PathBuf = "/path/to/where".parse().unwrap();

As I add different type annotations for the returned value, Rust can figure out what I want it to parse from the string. It’s amazing!.

You can see, I didn’t create a concrete type from some generic constructor like String<IpAddr>, and neither did I call a specific parse method like parseIpAddr. The main execution code is the same but only the return type differed. And Rust compiler is so proactive to find me the right complementation to do it right. Of course, it will complain if I mistype the type

I couldn’t recall any other language that can provide this same excellent development experience as Rust does.

Why am I so excited about this usage? Because

It’s predicted

I can even imagine that one day a crate/library comes out and it supports parsing some specified data structure from strings, the author will likely prefer to implement it in the way (under the hood, is to implement the FromStr trait), so that users can continue using the same kind of code. No need to learn another new API or method!

It can narrow down the whole set of everyday APIs in our daily work. It will significantly save our time spent on learning crates’ documents. We can learn once and apply it everywhere if only the community can emerge this kind of general trait more

--

--