4 ms·
I don't know if that's quite true. While Rust is very deep, I've found that you can get quite far with a few crucial pieces of info (note that I don't speak abo
by allengeorge 9y ago
I don't know if that's quite true. While Rust is very deep, I've found that you can get quite far with a few crucial pieces of info (note that I don't speak about generics or macros; haven't worked a lot with them):
* All variables are expected to be sized. So, learn what's sized and what isn't.
* Understand traits and how they add functionality to types. Have a small dictionary of common ones (From, Into, Debug, etc.)
* Learn how to write blanket implementations for traits. This can make your code lighter-weight. You also learn to start looking for blanket implementations.
* Encapsulate ownership details when possible. I'm not sure about the best way to explain this, but...at a high level it means "structure your types so that you avoid sharing ownership".
- JasonSage 9y agoAny links or ideas on how to get started with some of these? Understanding traits and knowing common ones is as trivial as reading the Rust Book, but ideas like "write blanket implementations" and "encapsulate ownership details" are less easy to find.
- allengeorge 9y agoI really started looking into blanket implementations after I saw this example [1] in Tokio. Basically it says "implement the `AsyncRead` trait for any boxed type that implements `AsyncRead` or any `AsyncRead` trait object. The Error trait contains more examples [2]. As for "encapsulate ownership details". Hard to describe, but...let me point to Tokio [3] again. In Java if you wanted to share an IO reader/writer with two different objects you'd use the same type and pass the same reference to both owners. In Rust, the same idiom would require the wordy use of `Arc<Mutex<Type>>` or `Rc<RefCell<Type>>`. I hope we can both agree that that's frustrating. An alternative (as demonstrated by Tokio's `split` method) is to separate your base type into two other types - reader and writer - that can be owned independently. No more externally-visible ref counting. Of course, this isn't possible in all scenarios: there absolutely are times when you don't have a clean separation of types and have to ref count; but, avoid it if you can. I learned all of this by poking into libs, asking a ton on the IRC channels (everyone is super friendly) and failing a lot. Any errors are my own. Hope that helps! [1] https://github.com/tokio-rs/tokio-io/blob/master/src/lib.rs#L192 https://github.com/tokio-rs/tokio-io/blob/master/src/lib.rs#... [2] https://doc.rust-lang.org/src/std/error.rs.html#281 https://doc.rust-lang.org/src/std/error.rs.html#281 [3] https://github.com/tokio-rs/tokio-io/blob/master/src/lib.rs#L186 https://github.com/tokio-rs/tokio-io/blob/master/src/lib.rs#...