speice.io/assets/js/130b4a4b.d6001e72.js

1 line
16 KiB
JavaScript
Raw Normal View History

"use strict";(self.webpackChunkspeice_io=self.webpackChunkspeice_io||[]).push([["294"],{66056:function(e,n,t){t.r(n),t.d(n,{assets:function(){return l},contentTitle:function(){return o},default:function(){return h},frontMatter:function(){return a},metadata:function(){return i},toc:function(){return c}});var i=t(47497),s=t(85893),r=t(50065);let a={slug:"2018/09/primitives-in-rust-are-weird",title:"Primitives in Rust are weird (and cool)",date:new Date("2018-09-01T12:00:00.000Z"),authors:["bspeice"],tags:[]},o=void 0,l={authorsImageUrls:[void 0]},c=[{value:"Defining primitives (Java)",id:"defining-primitives-java",level:2},{value:"Low Level Handling of Primitives (C)",id:"low-level-handling-of-primitives-c",level:2},{value:"impl primitive (and Python)",id:"impl-primitive-and-python",level:2},{value:"Conclusion",id:"conclusion",level:2}];function d(e){let n={a:"a",blockquote:"blockquote",code:"code",em:"em",h2:"h2",p:"p",pre:"pre",strong:"strong",...(0,r.a)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.p,{children:"I wrote a really small Rust program a while back because I was curious. I was 100% convinced it\ncouldn't possibly run:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",children:'fn main() {\n println!("{}", 8.to_string())\n}\n'})}),"\n",(0,s.jsx)(n.p,{children:"And to my complete befuddlement, it compiled, ran, and produced a completely sensible output."}),"\n",(0,s.jsxs)(n.p,{children:["The reason I was so surprised has to do with how Rust treats a special category of things I'm going to\ncall ",(0,s.jsx)(n.em,{children:"primitives"}),". In the current version of the Rust book, you'll see them referred to as\n",(0,s.jsx)(n.a,{href:"https://doc.rust-lang.org/book/second-edition/ch03-02-data-types.html#scalar-types",children:"scalars"}),", and in older versions they'll be called ",(0,s.jsx)(n.a,{href:"https://doc.rust-lang.org/book/first-edition/primitive-types.html",children:"primitives"}),", but\nwe're going to stick with the name ",(0,s.jsx)(n.em,{children:"primitive"})," for the time being. Explaining why this program is so\ncool requires talking about a number of other programming languages, and keeping a consistent\nterminology makes things easier."]}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"You've been warned:"})," this is going to be a tedious post about a relatively minor issue that\ninvolves Java, Python, C, and x86 Assembly. And also me pretending like I know what I'm talking\nabout with assembly."]}),"\n",(0,s.jsx)(n.h2,{id:"defining-primitives-java",children:"Defining primitives (Java)"}),"\n",(0,s.jsxs)(n.p,{children:["The reason I'm using the name ",(0,s.jsx)(n.em,{children:"primitive"})," comes from how much of my life is Java right now. For the most part I like Java, but I digress. In Java, there's a special\nname for some specific types of values:"]}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{children:"bool char byte\nshort int long\nfloat double\n"})}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["They are referred to as ",(0,s.jsx)(n.a,{href:"https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html",children:"primitives"}),". And relative to the other bits of Java,\nthey have two unique features. First, they don't have to worry about the\n",(0,s.jsx)(n.a,{href:"https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions",children:"billion-dollar mistake"}),";\nprimitives in Java can never be ",(0,s.jsx)(n.code,{children:"null"}),". Second: ",(0,s.jsx)(n.em,{children:"they can't have instance methods"}),".\nRemember that Rust program from earlier? Java has no idea what to do with it:"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-java",children:"class Main {\n public static void main(String[] args) {\n int x = 8;\n System.out.println(x.toString()); // Triggers a compiler error\n }\n}\n"})}),"\n",(0,s.jsx)(n.p,{children:"The error is:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{children:"M