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 variablexwith the value5.let x_addr = std::ptr::addr_of(&x);: Calls thestd::ptr::addr_offunction with a reference toxas an argument, and assigns the address ofxto thex_addrvariable.println!("x is at address {:p}", x_addr);: Prints the address ofxusing the{:p}format specifier.
Helpful links
Related
- How do I zip variables in Rust?
- How do I write a variable to a file in Rust?
- How do I check if a variable is in a list of values in Rust?
- How do I use a range with a variable in Rust?
- How do I print the type of a variable in Rust?
- How do I reuse a variable in Rust?
- How do I print a variable in Rust?
- How do I identify unused variables in Rust?
- What is an environment variable in Rust?
- How do I access a tuple variable by index in Rust?
More of Rust
- How to match whitespace with a regex in Rust?
- How to use regex lookahead in Rust?
- How to use Unicode in a regex in Rust?
- How to replace a capture group using Rust regex?
- How to ignore case in Rust regex?
- How to match a URL with a regex in Rust?
- How to replace strings using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to use negation in Rust regex?
- Regex example to match multiline string in Rust?
See more codes...