this post was submitted on 21 Apr 2024
66 points (97.1% liked)

Rust

6030 readers
1 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

[email protected]

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS
66
submitted 7 months ago* (last edited 7 months ago) by larix to c/rust
 

This is my first project in rust, called: since. A small tool to filter logfiles or any other files with a timestamp in it, by a given timestamp.

For example:

since 1h /var/log/messages prints all lines from the last hour. since 00:00 /var/log/messages prints all lines since 00:00.

It would be nice if you could take a look at the code. What would you change, what should I change?

I’m not a professional programmer, this is just a hobby, but I want to learn as much as possible.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 7 months ago (2 children)

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

[–] larix 3 points 7 months ago

 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.

Yes, that makes sense too. Great!

I’ve updated the code as recommended.

@[email protected] @[email protected]