1
fn main() {
    println!("{:p}", std::mem::size_of::<*const u32>());
}

当我在操场上尝试这个时它失败了:

error[E0277]: the trait bound `usize: Pointer` is not satisfied
 --> src/main.rs:2:22
  |
2 |     println!("{:p}", std::mem::size_of::<*const u32>());
  |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Pointer` is not implemented for `usize`
  |
  = note: required by `std::fmt::Pointer::fmt`
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

有没有办法打印原始指针的大小*const u32

4

1 回答 1

3
println!("{}", std::mem::size_of::<*const u32>());

{:p}要求待打印事物的结果满足Pointer,但size_of返回usize不满足的结果Pointer。您可以usize简单地使用{}.

{:p}本质上是用来打印内存位置的,不是size_of返回的东西。

于 2020-11-24T08:17:00.520 回答