Non-tutorial suggestion: I’ve you’re stuck, put a demonstration of your problem on the rust playground, post it here with the question. People in rustland are generally very willing to help out, and the playground is a very helpfull tool for that.
Non-tutorial suggestion: I’ve you’re stuck, put a demonstration of your problem on the rust playground, post it here with the question. People in rustland are generally very willing to help out, and the playground is a very helpfull tool for that.
Enums/Structs first, but those 2 are mixed, and any impl for them will be directly after the definition of the type itsef. Free functions last.
Hmm, right. I think it still might be warranted in niche cases, but trying to think of such a case made it pretty protracted in my head… maybe when functions can also be called for side effects, and the into
conversion is costly and the caller might not care about the return value?
Note that when you change num
to take &self
instead, this works out (you also need to mark foo
as mutable, of course).
2
was good, thanks. 4
needs a tad more thought imho, returning an impl T
does have its place, because it makes changing the return type of the function a non-breaking change.
While funny, this also highlights part of why I like rust’s error handling story so much: You can really just read the happy path and understand what’s going on. The error handling takes up minimal space, yet with one glance you can see that errors are all handled (bubbled up in this case). The usual caveats still apply, of course ;)