pub trait Pointer { // Required method fnfmt(&self, f: &mutFormatter<'_>) ->Result<(),Error>;}Expand description
p formatting.
ThePointer trait should format its output as a memory location. This is commonly presentedas hexadecimal. For more information on formatters, seethe module-level documentation.
Printing of pointers is not a reliable way to discover how Rust programs are implemented.The act of reading an address changes the program itself, and may change how the data is representedin memory, and may affect which optimizations are applied to the code.
The printed pointer values are not guaranteed to be stable nor unique identifiers of objects.Rust allows moving values to different memory locations, and may reuse the same memory locationsfor different purposes.
There is no guarantee that the printed value can be converted back to a pointer.
§Examples
Basic usage with&i32:
ImplementingPointer on a type:
usestd::fmt;structLength(i32);implfmt::PointerforLength {fnfmt(&self, f:&mutfmt::Formatter<'_>) -> fmt::Result {// use `as` to convert to a `*const T`, which implements Pointer, which we can useletptr =selfas*constSelf; fmt::Pointer::fmt(&ptr, f) }}letl = Length(42);println!("l is in memory here: {l:p}");letl_ptr =format!("{l:018p}");assert_eq!(l_ptr.len(),18);assert_eq!(&l_ptr[..2],"0x");Required Methods§
1.0.0 ·Sourcefnfmt(&self, f: &mutFormatter<'_>) ->Result<(),Error>
fnfmt(&self, f: &mutFormatter<'_>) ->Result<(),Error>
Formats the value using the given formatter.
§Errors
This function should returnErr if, and only if, the providedFormatter returnsErr.String formatting is considered an infallible operation; this function onlyreturns aResult because writing to the underlying stream might fail and it mustprovide a way to propagate the fact that an error has occurred back up the stack.
Implementors§
impl<F>Pointer for Fwhere F:FnPtr,
impl<Ptr>Pointer forPin<Ptr>where Ptr:Pointer,
impl<T>Pointer for*const Twhere T: ?Sized,
impl<T>Pointer for*mut Twhere T: ?Sized,
impl<T>Pointer for&Twhere T: ?Sized,
impl<T>Pointer for&mut Twhere T: ?Sized,
impl<T>Pointer forNonNull<T>where T: ?Sized,
impl<T>Pointer forAtomicPtr<T>
target_has_atomic_load_store=ptr only.