#ownership question

9 messages · Page 1 of 1 (latest)

covert mural
#

It doesn't exactly clone the argument. It does, however, only use it for &self methods

#

(namely, the fmt method of the appropriate formatting trait)

#

No.

#

The functionality of the fmt methods, in general, is to write the given value to a given buffer. format! creates a string to act as a buffer and then returns it after the write.

#

If you write a string to a string, the effect is equivalent to what clone would have done

#

But no clone was actually ever called, technically.

#

It's... Slightly more involved, but the basic idea is there.

It derefs the string to a &str, then it copies all of its bytes into the new buffer, then it returns the buffer

#

This is also what clone would do, so they end up doing the same thing

#

A "buffer" is generally a term for "temporary storage" or for "a block of memory". In case of IO, it's usually a block of memory, generally represented as one of Vec<u8> or &[u8] or &mut [u8] depending on what you're doing with it.

For format specifically, it's a String, because it knows you're writing through the std::fmt APIs instead of the std::io ones, and those are string-based.