{"id":2744,"date":"2022-04-03T15:25:40","date_gmt":"2022-04-03T09:55:40","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2744"},"modified":"2022-04-03T15:25:40","modified_gmt":"2022-04-03T09:55:40","slug":"rust-trait-vs-c-abstract-class","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/04\/03\/rust-trait-vs-c-abstract-class\/","title":{"rendered":"Rust Trait vs. C++ Abstract Class"},"content":{"rendered":"<p><em><strong>Traits<\/strong><\/em> in <em><strong>Rust<\/strong><\/em> are an amazing feature and contribute significantly to the expressive power of the language. For someone coming to <em><strong>Rust<\/strong><\/em> with a <em><strong>C++<\/strong><\/em> background (like me), <em><strong>Traits<\/strong><\/em> appear to be quite similar to <em><strong>Abstract Classes<\/strong><\/em> in C++. Although they are similar, <em><strong>Traits<\/strong><\/em> have certain characteristics that set them apart. In this article, I will try to compare the two and highlight the key differences.<\/p>\n<p>In order to keep the discussion simple and to the point, I will ignore <em><strong>&#8220;Generics&#8221; <\/strong><\/em>\u00a0in<em><strong> Rust (C++ Templates)<\/strong><\/em>.<\/p>\n<p>The first similarity is that both <em><strong>Traits<\/strong><\/em> and <em><strong>Abstract Classes<\/strong><\/em> cannot be instantiated directly. In that sense, we can say that both of them <em><strong>&#8220;prescribe&#8221;<\/strong><\/em> behavior, to be implemented by a conforming abstraction.<\/p>\n<p>Let us start with a simple example in <em><strong>Rust<\/strong><\/em>, where we define a <em><strong>&#8220;struct&#8221;<\/strong><\/em> and two <em><strong>&#8220;traits&#8221;<\/strong><\/em>.<\/p>\n<figure id=\"attachment_2745\" aria-describedby=\"caption-attachment-2745\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2745\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/04\/03\/rust-trait-vs-c-abstract-class\/rust-src1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src1.jpg\" data-orig-size=\"856,1317\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1648975905&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"rust-src1\" data-image-description=\"&lt;p&gt;Rust Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Rust Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src1-666x1024.jpg\" class=\"wp-image-2745\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src1.jpg?resize=550%2C846&#038;ssl=1\" alt=\"Rust Example\" width=\"550\" height=\"846\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src1.jpg?resize=195%2C300&amp;ssl=1 195w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src1.jpg?resize=666%2C1024&amp;ssl=1 666w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src1.jpg?resize=768%2C1182&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src1.jpg?w=856&amp;ssl=1 856w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2745\" class=\"wp-caption-text\"><strong>Rust Example<\/strong><\/figcaption><\/figure>\n<p>Please note that <em><strong>&#8220;MyStruct&#8221;<\/strong><\/em> does not implement any of the two traits yet (we will do this a little later). Another interesting point to note is that it is possible to define methods (not just declare them) in <em><strong>Traits<\/strong><\/em>. When we run this code, here is what we get:<\/p>\n<figure id=\"attachment_2746\" aria-describedby=\"caption-attachment-2746\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2746\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/04\/03\/rust-trait-vs-c-abstract-class\/rust-out1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out1.jpg\" data-orig-size=\"1204,200\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1648975948&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Rust Output\" data-image-description=\"&lt;p&gt;Rust Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Rust Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out1-1024x170.jpg\" class=\"wp-image-2746\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out1.jpg?resize=550%2C91&#038;ssl=1\" alt=\"Rust Output\" width=\"550\" height=\"91\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out1.jpg?resize=300%2C50&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out1.jpg?resize=1024%2C170&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out1.jpg?resize=768%2C128&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out1.jpg?w=1204&amp;ssl=1 1204w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2746\" class=\"wp-caption-text\"><strong>Rust Output<\/strong><\/figcaption><\/figure>\n<p>How can we implement similar functionality in <em><strong>C++<\/strong><\/em> using <em><strong>Abstract Classes<\/strong><\/em>? Pretty straightforward actually. Here is the code:<\/p>\n<figure id=\"attachment_2747\" aria-describedby=\"caption-attachment-2747\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2747\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/04\/03\/rust-trait-vs-c-abstract-class\/abstract-src1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src1.jpg\" data-orig-size=\"700,1122\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1648974475&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"C++ Example\" data-image-description=\"&lt;p&gt;C++ Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;C++ Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src1-639x1024.jpg\" class=\"wp-image-2747\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src1.jpg?resize=500%2C801&#038;ssl=1\" alt=\"C++ Example\" width=\"500\" height=\"801\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src1.jpg?resize=187%2C300&amp;ssl=1 187w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src1.jpg?resize=639%2C1024&amp;ssl=1 639w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src1.jpg?w=700&amp;ssl=1 700w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2747\" class=\"wp-caption-text\"><strong>C++ Example<\/strong><\/figcaption><\/figure>\n<p>Here also you can see that <em><strong>&#8220;MyStruct&#8221;<\/strong><\/em> does not (yet) use the two abstract classes <em><strong>&#8220;BaseTrait&#8221;<\/strong><\/em> and <em><strong>&#8220;DerivedTrait&#8221;<\/strong><\/em>. And, as in the case of <em><strong>&#8220;Traits&#8221;<\/strong><\/em>, it is possible to define methods in <em><strong>Abstract Classes<\/strong><\/em>. We get the following output when we run the program:<\/p>\n<figure id=\"attachment_2748\" aria-describedby=\"caption-attachment-2748\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2748\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/04\/03\/rust-trait-vs-c-abstract-class\/abstract-out1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out1.jpg\" data-orig-size=\"971,118\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1648974691&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"C++ Output\" data-image-description=\"&lt;p&gt;C++ Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;C++ Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out1.jpg\" class=\"wp-image-2748\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out1.jpg?resize=550%2C67&#038;ssl=1\" alt=\"C++ Output\" width=\"550\" height=\"67\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out1.jpg?resize=300%2C36&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out1.jpg?resize=768%2C93&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out1.jpg?w=971&amp;ssl=1 971w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2748\" class=\"wp-caption-text\"><strong>C++ Output<\/strong><\/figcaption><\/figure>\n<p>It is easy to see that the outputs of the <em><strong>Rust<\/strong><\/em> and <em><strong>C++<\/strong><\/em> programs match so far.<\/p>\n<p>Let us move on to the next step. How do we take advantage of <em><strong>Traits<\/strong><\/em> in <em><strong>Rust<\/strong><\/em>? We know that <em><strong>Traits<\/strong> <strong>&#8220;prescribe&#8221;<\/strong><\/em> behavior, which some abstraction might implement. So, here is our example with <em><strong>&#8220;MyStruct&#8221;<\/strong><\/em> implementing both the <em><strong>Traits<\/strong><\/em>:<\/p>\n<figure id=\"attachment_2749\" aria-describedby=\"caption-attachment-2749\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2749\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/04\/03\/rust-trait-vs-c-abstract-class\/rust-src2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src2.jpg\" data-orig-size=\"897,1224\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1648976193&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Rust &amp;#8211; Updated Example\" data-image-description=\"&lt;p&gt;Rust &amp;#8211; Updated Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Rust &amp;#8211; Updated Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src2-750x1024.jpg\" class=\"wp-image-2749\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src2.jpg?resize=550%2C751&#038;ssl=1\" alt=\"Rust - Updated Example\" width=\"550\" height=\"751\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src2.jpg?resize=220%2C300&amp;ssl=1 220w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src2.jpg?resize=750%2C1024&amp;ssl=1 750w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src2.jpg?resize=768%2C1048&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-src2.jpg?w=897&amp;ssl=1 897w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2749\" class=\"wp-caption-text\"><strong>Rust &#8211; Updated Example<\/strong><\/figcaption><\/figure>\n<p>What you must take note of is that we <span style=\"text-decoration: underline;\"><em>did not change the original definition of <strong>&#8220;MyStruct&#8221;<\/strong><\/em><\/span>, but added the extra behaviors independently.<span class=\"Apple-converted-space\">\u00a0 <\/span>This is pretty cool!<\/p>\n<p>Here is the output from the modified program:<\/p>\n<figure id=\"attachment_2750\" aria-describedby=\"caption-attachment-2750\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2750\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/04\/03\/rust-trait-vs-c-abstract-class\/rust-out2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out2.jpg\" data-orig-size=\"1202,365\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Rust &amp;#8211; Output After Change\" data-image-description=\"&lt;p&gt;Rust &amp;#8211; Output After Change&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Rust &amp;#8211; Output After Change&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out2-1024x311.jpg\" class=\"wp-image-2750\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out2.jpg?resize=550%2C167&#038;ssl=1\" alt=\"Rust - Output After Change\" width=\"550\" height=\"167\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out2.jpg?resize=300%2C91&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out2.jpg?resize=1024%2C311&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out2.jpg?resize=768%2C233&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/rust-out2.jpg?w=1202&amp;ssl=1 1202w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2750\" class=\"wp-caption-text\"><strong>Rust &#8211; Output After Change<\/strong><\/figcaption><\/figure>\n<p>What about the <em><strong>C++<\/strong><\/em> example? The typical scenario (let us not get into <em><strong>Design Patterns<\/strong><\/em> here) is to derive <em><strong>&#8220;MyStruct&#8221;<\/strong><\/em> from <em><strong>&#8220;DerivedTrait&#8221;<\/strong><\/em> and implement the pure virtual methods <em><strong>&#8220;foo()&#8221;<\/strong> <\/em>and <em><strong>&#8220;bar()&#8221;<\/strong><\/em>. The modified version looks like this:<\/p>\n<figure id=\"attachment_2752\" aria-describedby=\"caption-attachment-2752\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src2-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2752\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/04\/03\/rust-trait-vs-c-abstract-class\/abstract-src2-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src2-1.jpg\" data-orig-size=\"697,641\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1648975202&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"C++ &amp;#8211; Updated Program\" data-image-description=\"&lt;p&gt;C++ &amp;#8211; Updated Program&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;C++ &amp;#8211; Updated Program&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src2-1.jpg\" class=\"wp-image-2752\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src2-1.jpg?resize=500%2C460&#038;ssl=1\" alt=\"C++ - Updated Program\" width=\"500\" height=\"460\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src2-1.jpg?resize=300%2C276&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-src2-1.jpg?w=697&amp;ssl=1 697w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2752\" class=\"wp-caption-text\"><strong>C++ &#8211; Updated Program<\/strong><\/figcaption><\/figure>\n<p>The output from the program, in this case, will be:<\/p>\n<figure id=\"attachment_2753\" aria-describedby=\"caption-attachment-2753\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2753\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/04\/03\/rust-trait-vs-c-abstract-class\/abstract-out2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out2.jpg\" data-orig-size=\"967,230\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1648975240&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"C++ Output from Updated Program\" data-image-description=\"&lt;p&gt;C++ Output from Updated Program&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;C++ Output from Updated Program&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out2.jpg\" class=\"wp-image-2753\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out2.jpg?resize=550%2C131&#038;ssl=1\" alt=\"C++ Output from Updated Program\" width=\"550\" height=\"131\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out2.jpg?resize=300%2C71&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out2.jpg?resize=768%2C183&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/04\/abstract-out2.jpg?w=967&amp;ssl=1 967w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2753\" class=\"wp-caption-text\"><strong>C++ &#8211; Output from Updated Program<\/strong><\/figcaption><\/figure>\n<p>The outputs from <em><strong>Rust<\/strong><\/em> and <em><strong>C++<\/strong><\/em> match this time too, confirming that the behaviors are the same.<\/p>\n<p>What is the main take-away from these two implementations? Which appeals to you more?<\/p>\n<p>My view is that declaring behaviors through <em><strong>Traits<\/strong><\/em> is more natural and flexible compared to doing the same through <em><strong>Abstract Classes<\/strong><\/em>. How so? As you can see from our example, <em><strong>&#8220;MyStruct&#8221;<\/strong> <\/em>can choose to take on additional behaviors <em><span style=\"text-decoration: underline;\">without changing its original definition<\/span><\/em>. In the case of <em><strong>C++<\/strong><\/em>, we had to change the definition of <em><strong>&#8220;MyStruct&#8221;<\/strong><\/em> to derive from <em><strong>&#8220;DerivedTrait&#8221;<\/strong><\/em> to take on its behaviors. What if <em><strong>&#8220;MyStruct&#8221;<\/strong><\/em> has to also implement additional behaviors? Of course, since <em><strong>C++<\/strong><\/em> supports multiple inheritance, this might not be a problem, but you can see that the implementation can get a bit convoluted. Other approaches exist, for example, using a <em><strong>&#8220;Behavioral Design Pattern&#8221;<\/strong><\/em>, but that is beyond the scope of this article.<\/p>\n<p>In summary, I feel that <em><strong>Traits<\/strong><\/em> in <em><strong>Rust<\/strong><\/em> combine the benefits of <em><strong>C++ Abstract Classes<\/strong><\/em>, <em><strong>Java Interfaces<\/strong><\/em> and <em><strong>Lisp Mixins<\/strong><\/em>.<\/p>\n<p>You can download the <em><strong>C++<\/strong><\/em> and <em><strong>Rust<\/strong><\/em> source files from <a href=\"http:\/\/www.rangakrish.com\/downloads\/RustVsCpp.zip\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>. I used <em><strong>Visual Studio 2022<\/strong><\/em> for the <em><strong>C++<\/strong><\/em> example and <em><strong>Intellij CLion 2021.3<\/strong><\/em> for the <em><strong>Rust<\/strong><\/em> example.<\/p>\n<p>Have a nice weekend and a great week ahead!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Traits in Rust are an amazing feature and contribute significantly to the expressive power of the language. For someone coming to Rust with a C++ background (like me), Traits appear to be quite similar to Abstract Classes in C++. Although they are similar, Traits have certain characteristics that set them apart. In this article, I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[49,17,314],"tags":[315,67,317,316],"class_list":["post-2744","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","category-rust","tag-abstract-class","tag-c","tag-rust","tag-trait"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-Ig","jetpack-related-posts":[{"id":1973,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/","url_meta":{"origin":2744,"position":0},"title":"std::is_base_of","author":"admin","date":"April 26, 2020","format":false,"excerpt":"In our on-going series on C++ Type Traits, today's topic is about the trait std::is_base_of<>. For the official description, see this. std::is_base_of<A, B>::value takes two arguments, both classes (or structs), and returns true if A is a base class of B and false otherwise. Trivially, std::<A, A>::value is true. Let\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2789,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/04\/using-dll-functions-in-rust\/","url_meta":{"origin":2744,"position":1},"title":"Using DLL Functions in Rust","author":"admin","date":"May 4, 2022","format":false,"excerpt":"When you program in Rust, especially in a non-trivial project, there is a good chance that you will need to call \"external\" functions (usually, C\/C++) that are available in a DLL (we are talking about the Windows platform here). It could be because you wish to re-use some code that\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"New DLL Project","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project-300x211.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project-300x211.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project-300x211.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2024,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/","url_meta":{"origin":2744,"position":2},"title":"std::is_standard_layout","author":"admin","date":"June 21, 2020","format":false,"excerpt":"In our last post, we learnt about the type trait std::is_trivial<T>. Today, let us go through another type trait that is quite similar. The expression\u00a0is_standard_layout<T>::value returns true if the layout of objects of type T is compiler independent, and hence is of standard format. Else, it returns false. This is\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"The Difference","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":293,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/08\/07\/is-deriving-from-a-concrete-class-bad\/","url_meta":{"origin":2744,"position":3},"title":"Is Deriving from a Concrete Class Bad?","author":"admin","date":"August 7, 2016","format":false,"excerpt":"In my first post on Julia, I noted that the language does not allow deriving from a concrete (i.e., non-abstract) class. It definitely came as a surprise because in most OO languages (C++, Java, Scala, C#, etc.) such a restriction does not exist. It is true that when you design\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1928,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","url_meta":{"origin":2744,"position":4},"title":"std::is_destructible","author":"admin","date":"March 15, 2020","format":false,"excerpt":"In the last article, I explained the deleted destructor in some detail. Today, I would like to talk about a related construct, a type trait called std::is_destructible. Type traits, defined in the header <type_traits>, are a big help when it comes to implementing template metaprogramming. \u00a0See this\u00a0article for a nice\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Private\/Protected Destructor","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":4012,"url":"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/","url_meta":{"origin":2744,"position":5},"title":"Common Lisp Metaobject Protocol: Classes are Just Objects!","author":"admin","date":"January 1, 2026","format":false,"excerpt":"In today\u2019s popular languages such as C++, Java, Golang, Rust, Python, etc., classes are fixed constructs defined by the language. They have a definite syntax that can\u2019t be changed while programming. We are all used to this of course. But what makes Common Lisp stand out is that in that\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"A Simple Class","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code1-300x42.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2744"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2744\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}