【问题标题】:Why does printing a pointer print the same thing as printing the dereferenced pointer?为什么打印指针与打印取消引用的指针打印相同的东西?
【发布时间】:2015-03-07 07:18:54
【问题描述】:

来自 Rust 指南:

要取消引用(获取被引用的值而不是引用本身)y,我们使用星号 (*)

所以我做到了:

fn main() {
    let x = 1;
    let ptr_y = &x;
    println!("x: {}, ptr_y: {}", x, *ptr_y);
}

即使没有显式取消引用,这也会给我相同的结果 (x=1; y=1):

fn main() {
    let x = 1;
    let ptr_y = &x;
    println!("x: {}, ptr_y: {}", x, ptr_y);
}

为什么? ptr_y不应该打印内存地址,*ptr_y不应该打印1吗?是否有某种自动取消引用或我错过了什么?

【问题讨论】:

    标签: pointers rust dereference


    【解决方案1】:

    Rust 通常关注对象值(即内容中有趣的部分)而不是对象标识(内存地址)。 implementation of Display for &T 其中T 实现Display 直接遵循内容。为DisplayString 实现手动扩展该宏:

    impl<'a> Display for &'a String {
        fn fmt(&self, f: &mut Formatter) -> Result {
            Display::fmt(&**self, f)
        }
    }
    

    也就是说,它只是直接打印它的内容。

    如果您关心对象身份/内存地址,可以使用Pointer formatter{:p}

    fn main() {
        let x = 1;
        let ptr_y = &x;
        println!("x: {}, ptr_y: {}, address: {:p}", x, ptr_y, ptr_y);
    }
    

    输出:

    x: 1, ptr_y: 1, address: 0x7fff4eda6a24
    

    playground

    【讨论】:

    • 我在您发布后不久在 Rust 指南中阅读了有关指针的信息,他们只是使用“{:p}, ptr_y”,因为“println!会自动为我们取消引用它”。一个简短的问题:我通常使用“Type name = value”或“int test = 1”之类的东西。似乎我不能在 Rust 中将它作为选项,“let int test = 1”会产生编译器错误?我知道编译器会自动获取类型,但为了我的语法,我想在声明时将它写在我的代码中:(。无论如何,即使我不知道宏到底在做什么,也要感谢你的回答:)。
    • 哦,是的,{:p} 也可以,p format specifierimplemented for various kinds of pointers 只打印地址。
    • 很好,这就是我需要知道的一切:)。现在我需要掌握更大的东西,我只习惯于 OO 并且不知道之后发生的任何事情(doc.rust-lang.org/book/intermediate.html),尤其是模板和通用的东西。感谢您的帮助。
    【解决方案2】:
    fn main() {
     let x = &42;
     let address = format!("{:p}", x); // this produces something like '0x7f06092ac6d0'
     println!("{}", address);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 2011-01-29
      相关资源
      最近更新 更多