-
Book Overview & Buying
-
Table Of Contents
Learning Rust
By :
Consider the following piece of code:
fn main()
{
let a = 32;
let b = &a;
} We have created two variable bindings, with the second one (b) pointing at the address for a. The b variable doesn't contain the value of the a variable, but it points to the position a is held at, from which it can obtain a value (in other words, the value of b is borrowed from a).
In terms of our stack diagram, we have this:
Function name | Address | Variable name | Value |
|
|
|
|
|
|
|
If we have a function call another function, but with a parameter, our stack will look slightly different:
fn second(i: &i32)
{
let c = 42;
println!("{}", *i);
}
fn main()
{
let a = 32;
let b = &a;
second(b);
}
Function name | Address | Variable name | Value |
|
|
| |
second |
|
|
|
|
|
| |
main |
|
|
|
The i binding points to address and the 0b variable points to address , and this is the...0