Hacker News new | ask | show | jobs
by nindalf 704 days ago
All of these options work and are equivalent.

- let time = Timestamp::parse("2024-07-11T01:14:00Z")?;

- let time: Timestamp = "2024-07-11T01:14:00Z".parse()?;

- let time = "2024-07-11T01:14:00Z".parse::<Timestamp>()?;

You’re free to choose whatever you prefer, although the compiler needs to be able to infer the type of time. If it can’t, it’ll let you know.

So a fourth option is allowed, as long as the subsequent lines make the type of time unambiguous.

- let time = "2024-07-11T01:14:00Z".parse()?;

This is a direct consequence of Timestamp implementing the FromStr trait.

1 comments

  let time = Timestamp::from_str("2024-07-11T01:14:00Z")?;
I think you meant :)
Haha, yes I did. If only the HN textbox integrated with rust-analyzer, it would have caught the mistake.