How to print the owner of a value in Rust?

Hi, is there a way to print the owner of a value in Rust ?

What kind of output are you specifically looking for?

Not that I know of.

Except the compiler will produce nice error messages if non-owner is trying to do something with data that only an owner can do. Those messages tell you.

I'm wondering why you would need to know in the case where everything is compiling correctly?

1 Like

I did not think about it well before writing the question. I think there can be no way to know the owner of a value using a function or whatever.

Of course you would want to track how things will work in your head before running the code, but i think there is no way to do it via a function or something similar.

Indeed, ownership is not something values themselves care about. A String doesn't need to know whether it comes from a function, or it's in a struct field, or an enum variant, or in an array of strings, or wherever – it needs to operate correctly in all of these cases.

If the type system cared about that, then types couldn't be used uniformly at different places, and we would lose local compositionality of code. We'd be in deep trouble then.

I think you might have an incorrect mental model of ownership. Ownership isn't something where, say, every object has a pointer back to its owner and things check against that.

It's purely a compile-time check, so there's no size or instruction overhead at runtime. And that's really important, because it means that you can have Vec<&str> where those strs are owned by a whole bunch of different places, without overhead.

But because of that, there's no way to, say, find the String that owns the data behind a &str -- after all, it might be a literal, so the data's in the program, or come from a stack array, or …

5 Likes

Well, basically, if you print the borrowed value using Display, you print exactly the owner’s value.

let a = 126u8;
let ref b = a;
println!("{b}"); // 126

But I think the OP wants to know the owner of the value, not the value itself. Something like:

println!({:?}, "someData.owner());

Which would produce, perhaps, the name of the binding, the function that binding is in, perhaps the thread that function is running from....what else?

I'm curious to know what use can be made of such information at run time?

It would definitely be useful for debugging, eg. multiple ownership (eg. Rc). (Alas, that means that there is no such thing as the owner in this case, or at least which particular Rc instance we regard as the owner is ambiguous at best.)

However, given that I haven't once needed to use the debugger ever since I began working in Rust, I suspect that the same level of usefulness is provided by the compiler in the form of error messages – so while not useless, storing this kind of runtime information would be at best redundant.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.