Div trait is not implemented

This code compiled by cargo build:

extern crate time;

fn main() {
	let t1 = time::precise_time_ns();
	let t2 = time::precise_time_ns();

    println!("Time elapsed: {} ms", (t2 - t1)/1000000.0);

does not work:

main.rs:28:37: 28:56 error: the trait `core::ops::Div<_>` is not implemented for the type `u64` [E0277]
main.rs:28     println!("Time elapsed: {} ms", (t2 - t1)/1000000.0);

There is missing the trait I suppose but don't know how to find it.
Tried already without success:

use std::ops::Div;

Rust does very little in terms of implicit type converting so you need to do

println!("Time elapsed: {} ms", (t2 - t1) as f32 / 1000000.0);

Using f64 may also be what you want to have.

Indeed, (t2 - t1) as f32 / 1000000 doesn't work too. Do you have an idea how to implement custom core::ops::Div<_> operator for u64 and f64 params?

You don't.

You only get to write an impl for something if you defined either the type or the trait. In this case, you don't control either, so there's nothing you can do.

Rust isn't big on implicit conversions, or operations between different types that would involve implicit conversions.

It's simplest to just get used to casting. :slight_smile: