{"id":4180,"date":"2022-09-27T20:44:09","date_gmt":"2022-09-28T01:44:09","guid":{"rendered":"https:\/\/www.becomebetterprogrammer.com\/?p=4180"},"modified":"2022-09-27T20:53:26","modified_gmt":"2022-09-28T01:53:26","slug":"rust-print-raw-pointer","status":"publish","type":"post","link":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/rust-print-raw-pointer\/","title":{"rendered":"How to Print a Raw Pointer in Rust?"},"content":{"rendered":"\n<p>The other day I wrote an article about the <a href=\"https:\/\/www.becomebetterprogrammer.com\/rust-dereference-unary-operator\/\">dereference unary operator<\/a> (<code>*<\/code>) and I needed a way to explain the unary operator by first explaining about pointers in Rust, which are the memory address location of, for example, a variable. However, I didn&#8217;t know how to print the pointer of a variable, without displaying the variable&#8217;s value.<\/p>\n\n\n\n<p><strong>To print a raw pointer in Rust, call the <code>println!<\/code> or <code>print!<\/code> macro and  use the  syntax <code>{:p}<\/code> 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 <code>println!<\/code> or <code>print!<\/code> .<\/strong>  See the following example.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rust\"><code>let my_number = 1;\r\nprintln!(\"my_number memory location {:p}\", &amp;my_number);<\/code><\/pre>\n\n\n\n<p>You can store in a variable the pointer of a variable by using the coearce reference <code>&amp;T<\/code> o a variable to get the raw pointer of a given variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rust\"><code>let my_number = 1;\nlet my_number_pointer: *const i32 = &amp;my_number;\nprintln!(\"my_number memory location {:p}\", my_number_pointer);<\/code><\/pre>\n\n\n\n<p>In a similar way, you can print the raw pointer of a variable by using the mutable reference <code>&amp;mut T<\/code> of a variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rust\"><code>let mut my_number = 1;\r\nprintln!(\"my_number memory location {:p}\", &amp;mut my_number);\n\n\/\/ or\nlet mut my_number = 1;\nlet my_number_pointer: *const i32 = &amp;mut my_number;\r\r\nprintln!(\"my_number memory location {:p}\", my_number_pointer);<\/code><\/pre>\n\n\n\n<p>There <a href=\"https:\/\/doc.rust-lang.org\/std\/primitive.pointer.html\" target=\"_blank\" rel=\"noopener\">are other ways to get raw pointers<\/a> such as<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Using the <code>into_raw<\/code> function to consume a box and return the raw pointer<\/li><li>Using the macro <code>ptrs::addr_of!<\/code> or <code>ptr::addr_of_mut!<\/code><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The other day I wrote an article about the dereference unary operator (*) and I needed a way to explain the unary operator by first explaining about pointers in Rust, which are the memory address location of, for example, a variable. However, I didn&#8217;t know how to print the pointer of a variable, without displaying &#8230; <a title=\"How to Print a Raw Pointer in Rust?\" class=\"read-more\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/rust-print-raw-pointer\/\" aria-label=\"More on How to Print a Raw Pointer in Rust?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":4379,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[31],"tags":[],"class_list":["post-4180","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-rust","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/4180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/comments?post=4180"}],"version-history":[{"count":3,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/4180\/revisions"}],"predecessor-version":[{"id":4378,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/4180\/revisions\/4378"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/media\/4379"}],"wp:attachment":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/media?parent=4180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/categories?post=4180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/tags?post=4180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}