Whenever I encounter an interesting Rust programming technique, I add it to this blog post. I've amassed a bit of a collection. Hopefully someone finds it interesting and useful!
Rust Pro Tips (collection)
https://geeklaunch.io/blog/rust-pro-tips-collection/
6 Comments
Sibbo@sopuli.xyz · 4 pts · 3y
These are actually nice, thanks!
calcopiritus@lemmy.world · 2 pts · 2y
In tip #3 I don't see any benefit of doing impl AsRef<[T]> over &[T]
internetionals@toot.community · 1 pts · 2y
@calcopiritus @hatchet That way you can pass a reference or anything that can be turned into a reference as an argument. So the caller can supply a &T, Box, Rc, Arc, … (I dont’t know if there is a blanket impl so that even T itself will work.
hatchet@sh.itjust.works · 1 pts · 2y
Well, actually I would tend to agree that &[T] is preferable to AsRef in most cases; all of the smart pointers you mentioned can also easily be turned into plain references. I probably could have chosen a better example.
ParanoidAndr0id@kbin.social · 1 pts · 3y
Bookmarked :)
pgsuper@lemmy.world · 0 pts · 3y
Nice job! I’d add that the target of the Rustdoc link shortcuts can be customized, in case they are not autodetected or point to an undesired location, like so:
That will make the word Tool point to that type (note that the namespaces there - in lib::types::Tool - are relative to the current module / context, so you can use an imported name directly there too, for example).