{"id":7741,"date":"2019-11-25T22:35:14","date_gmt":"2019-11-25T22:35:14","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7741"},"modified":"2019-11-25T22:35:14","modified_gmt":"2019-11-25T22:35:14","slug":"rust-modules","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/rust-modules\/","title":{"rendered":"Rust modules"},"content":{"rendered":"<p>Rust includes a module system for grouping code into logical groups, which may include structs, implementations, other modules etc. Module also give us the ability to manage module code&#8217;s visibility.<\/p>\n<p>There&#8217;s a couple of ways for declaring our modules.<\/p>\n<p><strong>Option 1<\/strong><\/p>\n<p>Assuming we&#8217;ve used <em>cargo init<\/em> to create our project or simply laid out our code in the same way, then we&#8217;ll have a <em>src<\/em> folder and within that we&#8217;ll create a folder named <em>math<\/em> which will become our module name. Within <em>math<\/em> we add a file named <em>mod.rs<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nsrc\r\n  |--- math\r\n    |--- mod.rs\r\n  |--- main.rs\r\n<\/pre>\n<p>So here&#8217;s a very basic <em>mod.rs<\/em> file<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\npub fn add(a: i32, b: i32) -&gt; i32 {\r\n    a + b\r\n}\r\n<\/pre>\n<p>and here&#8217;s the <em>main.rs<\/em> file<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nmod math;\r\n\r\nfn main() {\r\n    println!(&quot;{}&quot;, math::add(1, 2));\r\n}\r\n<\/pre>\n<p><strong>Option 2<\/strong><\/p>\n<p>The second option is to name the module file <em>math.rs<\/em> and this is stored at the same level as the <em>main.rs<\/em> file, i.e.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nsrc\r\n  |--- math.rs\r\n  |--- main.rs\r\n<\/pre>\n<p><strong>Nested Modules<\/strong><\/p>\n<p>We can also nest modules (modules within modules), for example<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\npub mod nested {\r\n  pub fn add(a: i32, b: i32) -&gt; i32 {\r\n    a + b\r\n  }\r\n}\r\n\r\n\/\/ in use \r\n\r\nmod math;\r\n\r\nfn main() {\r\n    println!(&quot;{}&quot;, math::nested::add(1, 2));\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Rust includes a module system for grouping code into logical groups, which may include structs, implementations, other modules etc. Module also give us the ability to manage module code&#8217;s visibility. There&#8217;s a couple of ways for declaring our modules. Option 1 Assuming we&#8217;ve used cargo init to create our project or simply laid out our [&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-7741","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\/7741","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=7741"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7741\/revisions"}],"predecessor-version":[{"id":7756,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7741\/revisions\/7756"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}