concept reference in category rust

appears as: references, reference, reference
Rust in Action

This is an excerpt from Manning's book Rust in Action.

Control over memory access, memory layout and specific CPU instructions is very important when squeezing the best performance out of code. At times, it is imperative to manage how something is operating. It might matter that data is stored in the stack, rather than on the heap. At times, it might make sense to add reference counting to a shared value. Often, it makes sense to pass references to functions. Occasionally, it might be useful to create one’s own type of pointer for a particular access pattern. Most of the time though, there are other things to worry about. And for the majority, Rust uses sensible defaults that align with its "zero cost abstractions" philosophy.

  • reference is used to signal that the Rust compiler will provide its safety guarantees
  • Listing 6. 1. Mimicing pointers with references. Two global variables, B and C are pointed to by references. Those references hold addresses of B and C respectively.
    static B: [u8; 10] = [99, 97, 114, 114, 121, 116, 111, 119, 101, 108];
    static C: [u8; 11] = [116, 104, 97, 110, 107, 115, 102, 105, 115, 104, 0];
    
    fn main() {
        let a = 42;
        let b = &B;  #1
        let c = &C;  #1
    
        println!("a: {}, b: {:p}, c: {:p}", a, b, c);  #2
    }