How to get current time in milliseconds in Rust?
Hello everyone! I just started learning Rust and encountered a task that is very simple in other languages, but causes difficulties here. I need to measure the execution time of some code section, and for that I want to get the current time in milliseconds (timestamp since the Unix epoch, January 1, 1970).
I wrote this code, but it doesn't compile:
```rust
use std::time;
fn main() {
let now = time::now();
let millis = now.elapsed().as_millis();
println!("Current time in ms: {}", millis);
}
```
The compiler complains that `time::now()` doesn't exist, and the `elapsed()` method is not suitable at all. I looked at the documentation, but there are many different structures: `SystemTime`, `Instant`, `Duration` — I'm confused. In Python I would write `time.time() * 1000`, in JavaScript — `Date.now`
No answers yet.
Log in to leave an answer