I have tried using raw pointer casts, like my_struct as *const usize
, but this results in a non-scalar cast
error. Raw pointers seem to work fine when finding the address of primitives, but not custom struct
s.
How to access struct properties (members) We access struct properties with dot notation. That means we separate the struct's variable name and its property name with the dot operator. In the example above, we print the properties from each struct by accessing their values with dot notation.
To get a specific value from a struct, we use dot notation. For example, to access this user's email address, we use user1. email . If the instance is mutable, we can change a value by using the dot notation and assigning into a particular field.
To print a raw pointer in Rust, call the println! or print! macro and use the syntax {:p} as part of the first string format argument. Finally, pass the variable you want to see the raw pointer as the second argument of the macro println! or print! .
You need to use the &
operator to get the address of any variable, so you need to write &my_struct as *const _
(where _
can be a literal _
, or the type of the value behind the pointer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With