No, Rust does not have any automatically generated ID for objects. That kind of functionality would incur some overhead for every user, and Rust wants to impose as little overhead as it needs to. Everything else should be opt-in.
As far as I know, the address of an item is as unique as you can get:
struct SomeStruct;
fn main() {
let some_thing = SomeStruct;
println!("{:p}", &some_thing);
let another = some_thing;
println!("{:p}", &another);
}
0x7ffc020ba638
0x7ffc020ba698
Everything1 takes up space somewhere, so you can get the address of that space and print that.
This might be too unique for some cases. For example, when you transfer ownership of an item, you might expect that the ID stays the same. I think in that case, you'd have to roll your own. Something like a global atomic variable that you can pull from when you create the object. Such a scheme won't apply to objects you don't control.
1 — Well, almost everything. I know that const
items aren't guaranteed to have a location, which is why static
items exist.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…