66
since: my first project
(codeberg.org)
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Credits
Hmm, interesting. The documentation tells me, it creates a new Option value, and allocating memory every time someone just wants to look at a value could be pretty bad.
But I guess, an Option of a reference never needs to allocate memory, because it'll either be a pointer to a value (
Some
) or a pointer to null (None
). Right?Well, that explains why it's technically possible.
As for why
Option<&str>
is preferrable then:It hides away your internals. Your caller should only care whether they can get the value or not (
Some
vs.None
), not what the precise reason is. That reason or your internal structure might change.@larix
Yes, that makes sense too. Great!
I’ve updated the code as recommended.
@Ephera@lemmy.ml @jcbritobr@mastodon.social