Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Compare memory address (pointer)

  • std::ptr::addr_of

  • addr_of

  • Another way to show that different pieces of strings are located in different places in the memory.

fn main() {
    let text_a = "Hello, world!";
    let text_b = "Hello, world!";
    println!("{}", text_a == text_b);
    println!(
        "{}",
        std::ptr::addr_of!(text_a) == std::ptr::addr_of!(text_b)
    );
    println!("{:?}", &std::ptr::addr_of!(text_a));
    println!("{:?}", &std::ptr::addr_of!(text_b));
    println!();

    let text_a = String::from("Hello, world!");
    let text_b = String::from("Hello, world!");
    println!("{}", text_a == text_b);
    println!(
        "{}",
        std::ptr::addr_of!(text_a) == std::ptr::addr_of!(text_b)
    );
    println!("{:?}", &std::ptr::addr_of!(text_a));
    println!("{:?}", &std::ptr::addr_of!(text_b));
}
true
false
0x7ffddbd50e38
0x7ffddbd50e50