{"id":7723,"date":"2019-11-23T20:31:13","date_gmt":"2019-11-23T20:31:13","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7723"},"modified":"2019-11-23T20:31:13","modified_gmt":"2019-11-23T20:31:13","slug":"println-structs","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/println-structs\/","title":{"rendered":"println! structs"},"content":{"rendered":"<p>So we&#8217;ve got ourselves a simply little struct<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct Point {\r\n    x: i32,\r\n    y: i32\r\n}\r\n<\/pre>\n<p>We then decide that we&#8217;d like to output the current state of the Point using println!, so we write<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nfn main() {\r\n    let p = Point { x: 20, y: 3 };\r\n\r\n    println!(&quot;{}&quot;, p);\r\n}\r\n<\/pre>\n<p>Running this will result in <em>`Point` doesn&#8217;t implement `std::fmt::Display`<\/em> and <em>`Point` cannot be formatted with the default formatter<\/em>. In fact we do not really need to implement std::fmt::Display, we can just annotate our struct with <em>#[derive(Debug)]<\/em> and then use the println! formatters (:? or :#?), for example<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#&#x5B;derive(Debug)]\r\nstruct Point {\r\n    x: i32,\r\n    y: i32\r\n}\r\n\r\nfn main() {\r\n    let p = Point { x: 20, y: 3 };\r\n\r\n    println!(&quot;{:?}&quot;, p);\r\n}\r\n<\/pre>\n<p>The use of :? will result in the following output <em>Point { x: 20, y: 3 }<\/em> whereas :#? will display the values on lines of their own (a &#8220;prettier&#8221; formatter). Both :? and :#? are debug formatters, hence require the annotation <em>#[derive(Debug)]<\/em> or we can implement std::fmt::Debug, for example<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nimpl std::fmt::Debug for Point {\r\n    fn fmt(&amp;self, f: &amp;mut std::fmt::Formatter) -&gt; std::fmt::Result {\r\n        write!(f, &quot;(x is {}, y is {})&quot;, self.x, self.y)\r\n    }\r\n}\r\n<\/pre>\n<p>For situations where we simply want to create our own custom display (not just for Debug), then, as per the original error <em>`Point` doesn&#8217;t implement `std::fmt::Display`<\/em>, we would need to implement the std::fmt::Display trait, i.e.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nimpl std::fmt::Display for Point {\r\n    fn fmt(&amp;self, f: &amp;mut std::fmt::Formatter) -&gt; std::fmt::Result {\r\n        write!(f, &quot;(x is {}, y is {})&quot;, self.x, self.y)\r\n    }\r\n}\r\n<\/pre>\n<p>This means we no longer requiring the annotation or special formatters, hence our full code will look like this<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct Point {\r\n    x: i32,\r\n    y: i32\r\n}\r\n\r\nimpl std::fmt::Display for Point {\r\n    fn fmt(&amp;self, f: &amp;mut std::fmt::Formatter) -&gt; std::fmt::Result {\r\n        write!(f, &quot;(x is {}, y is {})&quot;, self.x, self.y)\r\n    }\r\n}\r\n\r\nfn main() {\r\n    let p = Point { x: 20, y: 3 };\r\n\r\n    println!(&quot;{}&quot;, p);\r\n}\r\n<\/pre>\n<p>and as you&#8217;d expect our output is now <em>(x is 20, y is 3)<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So we&#8217;ve got ourselves a simply little struct struct Point { x: i32, y: i32 } We then decide that we&#8217;d like to output the current state of the Point using println!, so we write fn main() { let p = Point { x: 20, y: 3 }; println!(&quot;{}&quot;, p); } Running this will result [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[191],"tags":[],"class_list":["post-7723","post","type-post","status-publish","format-standard","hentry","category-rust"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7723"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7723\/revisions"}],"predecessor-version":[{"id":7732,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7723\/revisions\/7732"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}