Trait std::fmt::Pointer

1.0.0 · source ·
pub trait Pointer {
    // Required method
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>;
}
Expand description

p 格式。

Pointer trait 应该将其输出格式化为存储位置。 通常以十六进制表示。

有关格式化程序的更多信息,请参见 模块级文档

Examples

&i32 的基本用法:

let x = &42;

let address = format!("{x:p}"); // 这会产生类似 '0x7f06092ac6d0' 的东西
Run

在类型上实现 Pointer

use std::fmt;

struct Length(i32);

impl fmt::Pointer for Length {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // 使用 `as` 转换为 `*const T`,该 `*const T` 实现了 Pointer,我们可以使用它

        let ptr = self as *const Self;
        fmt::Pointer::fmt(&ptr, f)
    }
}

let l = Length(42);

println!("l is in memory here: {l:p}");

let l_ptr = format!("{l:018p}");
assert_eq!(l_ptr.len(), 18);
assert_eq!(&l_ptr[..2], "0x");
Run

Required Methods§

source

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

使用给定的格式化程序格式化该值。

Implementors§

1.4.0 · source§

impl<F> Pointer for Fwhere F: FnPtr,

1.33.0 · source§

impl<P> Pointer for Pin<P>where P: Pointer,

source§

impl<T> Pointer for *const Twhere T: ?Sized,

source§

impl<T> Pointer for *mut Twhere T: ?Sized,

source§

impl<T> Pointer for &Twhere T: ?Sized,

source§

impl<T> Pointer for &mut Twhere T: ?Sized,

1.25.0 · source§

impl<T> Pointer for NonNull<T>where T: ?Sized,

source§

impl<T> Pointer for Rc<T>where T: ?Sized,

1.24.0 · source§

impl<T> Pointer for AtomicPtr<T>

source§

impl<T> Pointer for Arc<T>where T: ?Sized,

source§

impl<T, A> Pointer for Box<T, A>where A: Allocator, T: ?Sized,