talks
talks copied to clipboard
Overview of type conversion traits and techniques in Rust (beginner)
One of the things that new Rust developers struggle with is how to handle conversions between types. For example, "I have a str
, how do I get a String
?" has several answers (and it's not necessarily the same patterns as going from a String
to a str
- e.g. .to_string()
is str
-to-String
specific), and it gets trickier if you want to return "str
or String
" (have to use Cow
, plus there's also the Borrow
and BorrowMut
traits which have subtly different use cases to the AsRef
and AsMut
traits.
It'd be great if someone could give an overview of this in a newbie friendly way; all the information is available in decent format for consumption so it should just be a matter of learning it yourself and then presenting on it.
On a related note, at least one of the conversion traits (From
) are also used for error handling with the try!
macro (and I believe the upcoming ?
syntax on Result
too) to cut down on boilerplate, so it's probably worth touching on that too? (and/or error handling techniques could be a separate talk too.)
Useful sources for people putting talks together:
- https://doc.rust-lang.org/std/convert/index.html
- https://ricardomartins.cc/2016/08/03/convenient_and_idiomatic_conversions_in_rust
- https://doc.rust-lang.org/book/borrow-and-asref.html
- https://doc.rust-lang.org/book/error-handling.html