rustHow do I print the address of a variable in Rust?
You can print the address of a variable in Rust using the std::ptr::addr_of
function. This function takes a reference to a variable as an argument and returns the address of the variable as a *const T
pointer.
Example code
let x = 5;
let x_addr = std::ptr::addr_of(&x);
println!("x is at address {:p}", x_addr);
Output example
x is at address 0x7ffc9f9f9f90
Code explanation
let x = 5;
: Declares a variablex
with the value5
.let x_addr = std::ptr::addr_of(&x);
: Calls thestd::ptr::addr_of
function with a reference tox
as an argument, and assigns the address ofx
to thex_addr
variable.println!("x is at address {:p}", x_addr);
: Prints the address ofx
using the{:p}
format specifier.
Helpful links
Related
- How do I identify unused variables in Rust?
- How do I increment a variable in Rust?
- How do I use a variable from another file in Rust?
- How do I check if a variable is in a list of values in Rust?
- How do I access a tuple variable by index in Rust?
- How do I add padding to a variable in Rust?
- How can I use a mutex as a global variable in Rust?
- How can I use a hashmap as a global variable in Rust?
- What type of variable declaration is used in Rust?
More of Rust
- How to modify an existing entry in a Rust HashMap?
- How to declare a matrix in Rust?
- Hashshet example in Rust
- How to parse JSON string in Rust?
- How to use a custom hash function with a Rust HashMap?
- How to yield return in Rust?
- How to convert a Rust slice of u8 to u32?
- How to convert a Rust slice to a fixed array?
- Yield example in Rust
- How to build a Rust HashMap from an iterator?
See more codes...