1

I'm trying to make a system that assigns unique identifiers to entities. I want part of the identity to be based on the time the entity was created and the other part to be based on RNG -- and I want the whole ID to be a number.

fn main() {
    let id_p1 = SystemTime:now();
}

How would I convert id_p1 to a usable number?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
muffledcry
  • 129
  • 1
  • 5

1 Answers1

4

The short answer is, "you don't." The SystemTime type doesn't implement any methods for converting into any integer types. There are probably good reasons for this. For example, there's no reason for the time to even be representable by an integer type. What if the current time is not a whole number of seconds since whatever reference is used?

If you really need an integer type out of this, you could compute the time elapsed since some defined reference, like the Unix epoch. This will yield a Duration, which does implement conversions to numeric types.

Another option might be to hash this time, possibly along with the salt from your RNG. The SystemTime does impl Hash, so you can feed the time and your random number, and generate a u64 as your ID.

bnaecker
  • 6,152
  • 1
  • 20
  • 33