Rust Pro Tips (collection)

3 years ago by hatchet to c/rust

Level up your Rust skills.

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!

Sibbo 4 points 3 years ago

These are actually nice, thanks!

path: 0 1748399, hotness: undefined, score: 4, children: 0
calcopiritus 2 points 3 years ago

In tip #3 I don't see any benefit of doing impl AsRef<[T]> over &[T]

path: 0 3253325, hotness: undefined, score: 2, children: 2
internetionals 1 point 3 years ago

@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.

path: 0 3253325 3256779, hotness: undefined, score: 1, children: 1
hatchet 1 point 3 years ago

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.

path: 0 3253325 3256779 3321534, hotness: undefined, score: 1, children: 0
ParanoidAndr0id 1 point 3 years ago

Bookmarked :)

path: 0 1763475, hotness: undefined, score: 1, children: 0
pgsuper 0 points 3 years ago

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:

/// Use a [Tool]
///
/// [Tool]: lib::types::Tool

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).

path: 0 1763900, hotness: undefined, score: 0, children: 0
rust
rust

@lemmy.ml

login for more options
9404
398
101

go to feed...