{"id":21018,"date":"2018-03-02T12:15:07","date_gmt":"2018-03-02T10:15:07","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=21018"},"modified":"2018-03-02T10:57:50","modified_gmt":"2018-03-02T08:57:50","slug":"rust-java-devs-structs","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/","title":{"rendered":"Rust for Java Devs \u2013 Structs"},"content":{"rendered":"<p>Next up in Rust for Java Devs we have structs. They are used to hold data within a logical unit that can be passed to other functions or execute their own methods on the values that they store. Now this sounds very familiar\u2026 Java objects do the same thing. For example if you took a POJO (Plain Old Java Object) you also pass it to other methods or execute the object\u2019s own methods. In this nature they are alike, but they do have their differences. In this post we will look into creating structs, retrieving their values, defining their own methods and how to execute them.<\/p>\n<h3>Creating a struct<\/h3>\n<p>Let\u2019s start with creating a struct.<\/p>\n<pre class=\"brush:php\">struct Person {\r\n  first_name: String,\r\n  last_name: String,\r\n  age: u32,\r\n  weight: u32,\r\n  height: u32,\r\n}<\/pre>\n<p>Thats all there is to it. The name of the struct is <code>Person<\/code> which has 5 fields inside of it. The nice thing about how structs work compared to Java\u2019s objects is that no constructor equivalent needs to be defined to create struct (until accessability is taken into account). You simply list out the fields and it will just work.<\/p>\n<p>Let\u2019s quickly compare the above Rust struct to a Java object.<\/p>\n<pre class=\"brush:java\">class Person {\r\n  private String firstName;\r\n  private String lastName;\r\n  private int age;\r\n  private int weight;\r\n  private int height;\r\n\r\n  Person(String firstName, String lastName, int age, int weight, int height) {\r\n    this.firstName = firstName;\r\n    this.lastName = lastName;\r\n    this.age = age;\r\n    this.weight = weight;\r\n    this.height = height;\r\n  }\r\n}<\/pre>\n<p>I\u2019m sure the first thing you will notice is the much greater amount of code that is needed to achieve the same goal. We needed to first list the fields \/ properties and then write a constructor to create the object.<\/p>\n<p>There are actually still a few more differences between the two just from these two snippets but we\u2019ll get to those in a minute. This amount of extra code (boilerplate code) is one of the main criticisms that people have with Java\u2026 Leading to the most common word that I hear about Java being \u201cverbose\u201d.<\/p>\n<p>So now we have defined a struct, how do we instantiate one to use in our code? Quite simple really.<\/p>\n<pre class=\"brush:php\">let person = Person {\r\n  first_name: String::from(\"John\"),\r\n  last_name: String::from(\"Doe\"),\r\n  age: 50,\r\n  weight: 200,\r\n  height: 180,\r\n};<\/pre>\n<p>This looks deceptively like the definition of the struct itself. But this time rather than stating what data type is being used for each field we have passed values instead.<\/p>\n<p>And now in Java.<\/p>\n<pre class=\"brush:java\">Person person = new Person(\"John\", \"Doe\", 50, 200, 180);<\/pre>\n<p>As you can see the names of the fields have been omitted from the code when compared to Rust\u2019s structs. This can lead to Java constructors being slightly more difficult to use if there are lots of parameters of the same type as you could mistakenly order the input values and thus creating an object with incorrect values. A possible solution for this is to use the Builder pattern instead of using a constructor directly.<\/p>\n<p>I mentioned above there are a few more differences between the struct and object that we defined earlier, so let\u2019s look at them.<\/p>\n<p>What if I want to pass in values into a struct in a different order to how the fields are listed? Easy, the order doesn\u2019t matter.<\/p>\n<pre class=\"brush:php\">let person = Person {\r\n  age: 50,\r\n  first_name: String::from(\"John\"),\r\n  height: 180,\r\n  weight: 200,\r\n  last_name: String::from(\"Doe\"),\r\n};<\/pre>\n<p>What if I wanted to only define some fields? Mmmm, well your out of luck there. All fields need to be initialized when a struct is created. If we tried to compile the code below.<\/p>\n<pre class=\"brush:php\">let person = Person {\r\n  first_name: String::from(\"John\"),\r\n};<\/pre>\n<p>The compiler will output.<\/p>\n<pre class=\"brush:php\">error[E0063]: missing fields `age`, `height`, `last_name` and 1 other field in initializer of `Person`\r\n --&gt; src\\main.rs:2:16\r\n  |\r\n2 |   let person = Person {\r\n  |                ^^^^^^ missing `age`, `height`, `last_name` and 1 other field\r\n\r\nerror: aborting due to previous error<\/pre>\n<p>The error is self explanatory and states what \/ how many fields are missing. This is a decision Rust has made to make the language safer as it removes the possibility of a Null Pointer Exception \/ Error popping up as every value has to be set. Java does not have this requirement which can lead to null pointers occurring at run time from using an object\u2019s value that is still <code>null<\/code>.<\/p>\n<p>How do the two points mentioned above differ from Java? In Java, you can input parameters into a constructor in different orders and you also don\u2019t need to initialize every field in the object. But, this comes with a catch. You need to create a new constructor for each order or number of parameters that you wish to pass in, or as I mentioned earlier, you could use the Builder pattern.<\/p>\n<p>For example if we wanted to write constructors that would allow the two Rust snippets above to work, we would write.<\/p>\n<pre class=\"brush:java\">class Person {\r\n  \/\/ fields\r\n  Person(String firstName, String lastName, int age, int weight, int height) {\r\n    \/\/ original constructor\r\n  }\r\n  Person(int age, String firstName, int height, int weight, String lastName) {\r\n    \/\/ set values\r\n  }\r\n  Person(String firstName) {\r\n    \/\/ set firstName\r\n  }\r\n}<\/pre>\n<p>A <code>Person<\/code> object can now be instantiated 3 different ways, although the last constructor is a bit risky due to <code>null<\/code> values on most of the fields and could lead to null pointer exceptions.<\/p>\n<h3>Accessing struct values<\/h3>\n<p>Next up, we will look at retrieving values from a struct.<\/p>\n<pre class=\"brush:php\">let first_name = person.first_name;<\/pre>\n<p>Thats about it. Whereas in Java you would write.<\/p>\n<pre class=\"brush:java\">String firstName = person.firstName;<\/pre>\n<p>This snippet relies on the <code>firstName<\/code> field being public, but this is considered bad practice in Java so you would normally write the below instead.<\/p>\n<pre class=\"brush:java\">String firstName = person.getFirstName();<\/pre>\n<p>This allows you to keep <code>firstName<\/code> private which provides you the flexibility to change the internals of the object without breaking existing code as long as the <code>getFirstName<\/code> is still available.<\/p>\n<p>From my understanding of Rust structs, struct fields are private by default within a module (I know I haven\u2019t gone through modules yet). So if we make this simpler, if we ran the last Rust snippet in the same file as the struct was defined, it would work, if it was in a different file it would not. To make the fields publicly accessible we need to add the <code>pub<\/code> modifier onto each field, like so.<\/p>\n<pre class=\"brush:php\">pub struct Person {\r\n  pub first_name: String,\r\n  pub last_name: String,\r\n  age: u32,\r\n  weight: u32,\r\n  height: u32,\r\n}<\/pre>\n<p>Here, <code>pub<\/code> was added onto the struct itself so it can be referenced from outside the module. The fields <code>first_name<\/code> and <code>last_name<\/code> are also marked with <code>pub<\/code> making them the only fields that could be used outside of the module when using the <code>Person<\/code> struct. This actually causes a problem, we have only 2 public fields, so when we go to construct a new <code>Person<\/code> instance, it will fail. This is the double edged sword of accessibility, if we want to hide some fields to prevent access it also means we can\u2019t pass values into them on construction, because technically we don\u2019t \u201cknow\u201d about them. We will look at getting past this barrier later on.<\/p>\n<p>If we tried to create an instance of the struct shown above from outside it\u2019s module, we would get the following error.<\/p>\n<pre class=\"brush:php\">error[E0451]: field `age` of struct `blog_post_sandbox::people::Person` is private\r\n --&gt; src\\main.rs:8:5\r\n  |\r\n8 |     age: 50,\r\n  |     ^^^^^^^ field `age` is private\r\n\r\nerror[E0451]: field `weight` of struct `blog_post_sandbox::people::Person` is private\r\n --&gt; src\\main.rs:9:5\r\n  |\r\n9 |     weight: 200,\r\n  |     ^^^^^^^^^^^ field `weight` is private\r\n\r\nerror[E0451]: field `height` of struct `blog_post_sandbox::people::Person` is private\r\n  --&gt; src\\main.rs:10:5\r\n   |\r\n10 |     height: 180,\r\n   |     ^^^^^^^^^^^ field `height` is private\r\n\r\nerror: aborting due to 3 previous errors<\/pre>\n<h3>Mutability<\/h3>\n<p>If you wanted to change the value of a struct field after it\u2019s original creation, it must be marked with <code>mut<\/code> making it mutable. In Rust you cannot choose which fields are mutable and which are not, either all fields are mutable or none are.<\/p>\n<pre class=\"brush:php\">let mut person = Person {\r\n  first_name: String::from(\"John\"),\r\n  last_name: String::from(\"Doe\"),\r\n  age: 50,\r\n  weight: 200,\r\n  height: 180,\r\n};\r\nperson.first_name = String::from(\"Bob\");<\/pre>\n<p>This will compile and when ran will change the <code>first_name<\/code> fields value from \u201cJohn\u201d to \u201cBob\u201d.<\/p>\n<p>Just to prove I\u2019m not lying, if we omitted the <code>mut<\/code> keyword and wrote the below instead.<\/p>\n<pre class=\"brush:php\">let person = Person {\r\n  first_name: String::from(\"John\"),\r\n  last_name: String::from(\"Doe\"),\r\n  age: 50,\r\n  weight: 200,\r\n  height: 180,\r\n};\r\nperson.first_name = String::from(\"Bob\");<\/pre>\n<p>You would get the following compiler error.<\/p>\n<pre class=\"brush:php\">error[E0594]: cannot assign to immutable field `person.first_name`\r\n --&gt; src\\main.rs:9:3\r\n  |\r\n2 |   let person = Person {\r\n  |       ------ consider changing this to `mut person`\r\n...\r\n9 |   person.first_name = String::from(\"Bob\");\r\n  |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot mutably borrow immutable field\r\n\r\nerror: aborting due to previous error<\/pre>\n<p>Comparing this to Java, we would normally use a setter if we wanted to make changes to our objects. Like so.<\/p>\n<pre class=\"brush:java\">person.setFirstName(\"Bob\");<\/pre>\n<p>Using setters also makes it easy to control which fields we wish to be mutable. If we don\u2019t create a setter and the field is private, then the field\u2019s value can\u2019t change. Simple.<\/p>\n<h3>Methods and Associated Functions<\/h3>\n<p>We have now got to the point where we can make our structs actually do things.<\/p>\n<p>I personally think how methods are written in Rust are very different to those in Java. You can form your own opinion once we have gone through this section.<\/p>\n<p>To define methods in Rust we first need to create an implementation block.<\/p>\n<pre class=\"brush:php\">struct Person {\r\n  first_name: String,\r\n  last_name: String,\r\n  age: u32,\r\n  weight: u32,\r\n  height: u32,\r\n}\r\n\r\nimpl Person {\r\n  \/\/ methods go here\r\n}<\/pre>\n<p>It looks almost the same as the code we have seen throughout this post but with the addition of an implementation block that is created using the <code>impl<\/code> keyword along with the struct\u2019s name (in this case <code>Person<\/code>).<\/p>\n<p>Now we can start adding some methods into the block. I will omit the creation of the struct\u2019s fields from code snippets for now.<\/p>\n<pre class=\"brush:php\">impl Person {\r\n  pub fn full_name(&amp;self) -&gt; String {\r\n    [&amp;self.first_name.to_string(), \" \", &amp;self.last_name.to_string()].concat()\r\n  }\r\n}<\/pre>\n<p>Here we have a method that is tied to the struct that it is invoked on. <code>&amp;self<\/code> is what distinguishes this as an instance method as it has access to it\u2019s own values. <code>self<\/code> and <code>&amp;mut self<\/code> can be used instead while keeping it as an instance method. I still need to go into what these syntaxes mean, but in short, <code>&amp;self<\/code> allows you to use the struct\u2019s own values without effecting any other code (doesn\u2019t take ownership of the <code>Person<\/code> instance), <code>&amp;mut self<\/code> is the same but allows changes to the instance and <code>self<\/code> prevents any code that occurs after the method is invoked to use the <code>Person<\/code> instance that was used. Some of this might sound confusing, but that\u2019s due to my bad ordering of writing these posts\u2026 I should probably write about ownership sometime soon.<\/p>\n<p>The method has also been marked with <code>pub<\/code> so it can be used outside of it\u2019s module, it can be removed if access should be more restrictive.<\/p>\n<p>In general, it looks very similar to normal a Rust function.<\/p>\n<p>To call the this method we need to write.<\/p>\n<pre class=\"brush:php\">let full_name = person.full_name();<\/pre>\n<p>The <code>&amp;self<\/code> reference is taken as the <code>person<\/code> instance and does not need to be passed into the method manually. If we wanted to pass in another parameter into a method, it would look like.<\/p>\n<pre class=\"brush:php\">impl Person {\r\n  pub fn full_name_with_random_parameter(&amp;self, random_parameter: &amp;str) -&gt; String {\r\n    [&amp;self.first_name.to_string(), \" \", &amp;self.last_name.to_string(), \" \", random_parameter].concat()\r\n  }\r\n}<\/pre>\n<p>And invoked by.<\/p>\n<pre class=\"brush:php\">let random_full_name = person.full_name_with_random_parameter(\"I'm a string\");<\/pre>\n<p>If we take a moment to compare Rust\u2019s instance methods to Java\u2019s the biggest difference is the passing in of <code>&amp;self<\/code> into the method to be able to access it\u2019s fields. In Java you can access an object\u2019s owns fields directly or by using the <code>this<\/code> keyword (like <code>&amp;self<\/code> is used in Rust) and does not require any sort of reference to itself to be passed in as a parameter.<\/p>\n<p>The above method would simply look like this when written in Java.<\/p>\n<pre class=\"brush:java\">public String fullNameWithRandomParameter(final String randomParameter) {\r\n  return firstName + \" \" + lastName + randomParameter;\r\n}<\/pre>\n<p>or<\/p>\n<pre class=\"brush:java\">public String fullNameWithRandomParameter(final String randomParameter) {\r\n  return this.firstName + \" \" + this.lastName + randomParameter;\r\n}<\/pre>\n<p>So if we pass in <code>&amp;self<\/code> into a method it becomes an instance method, then what happens if we don\u2019t? Well, technically they become functions instead of methods, associated functions to be precise because they are functions that are \u201cassociated\u201d to a struct (such a helpful explanation\u2026). They don\u2019t require an instance to be invoked which might sound familiar coming from Java. That\u2019s right (I\u2019ll assume figured it out), they are like Java\u2019s static methods.<\/p>\n<p>So let\u2019s look at an example.<\/p>\n<pre class=\"brush:php\">impl Person {\r\n  pub fn new(first_name: String, last_name: String, age: u32, weight: u32, height: u32) -&gt; Person {\r\n    Person {\r\n      first_name: first_name,\r\n      last_name: last_name,\r\n      age: age,\r\n      weight: weight,\r\n      height: height,\r\n    }\r\n  }\r\n}<\/pre>\n<p>This method is effectively a constructor to create a new <code>Person<\/code> instance from outside of the <code>Person<\/code>\u2018s module. If you recall to earlier in this post (actually quite long ago now), I mentioned that if any struct fields are private then code outside of it\u2019s module cannot create any instances of the struct\u2026 Well, this is the solution. By using a public constructor function we can still control accessability without restricting where instances can be created.<\/p>\n<p>Comparing this function to the methods we looked at previously, the only real difference is that there is no reference to <code>&amp;self<\/code> and therefore no instance tied to the function.<\/p>\n<p>Calling an associated function is a little different from a struct method.<\/p>\n<pre class=\"brush:php\">let person = Person::new(String::from(\"John\"), String::from(\"Doe\"), 50, 200, 180);<\/pre>\n<p>As the function is not related to an instance it uses the struct\u2019s type of <code>Person<\/code> to invoke the function. Another difference is the <code>::<\/code> syntax used to execute it instead of the typical <code>.<\/code> for an instance method. Using this information we can now classify that <code>String::from<\/code> which has been used in most of the Rust examples in this post is an associated function. Allowing a new <code>String<\/code> struct to be created from a <code>&amp;str<\/code> string because the struct\u2019s single field is not publicly available.<\/p>\n<p>As a Java static method is the equivalent of associated function, let\u2019s take a look at one.<\/p>\n<pre class=\"brush:java\">public static void create(final String firstName, final String lastName, final int age, final int weight, final int height) {\r\n  return new Person(firstName, lastName, age, weight, height);\r\n}<\/pre>\n<p>Nothing particularly interesting to say about this, except for the name of the method. I needed to name this method <code>create<\/code> rather than <code>new<\/code> like in the Rust version, because <code>new<\/code> is a keyword in Java for creating new instances via constructors.<\/p>\n<p>One nice feature before we finish. When creating a struct, if you have a variable whose name matches a struct\u2019s field directly, it\u2019s value will be passed in without a need to directly assign the value to the field. I can\u2019t think of a nice way to explain it in English, so we will use the language we understand better, code!<\/p>\n<pre class=\"brush:php\">impl Person {\r\n  pub fn new(first_name: String, last_name: String, age: u32, weight: u32, input_height: u32) -&gt; Person {\r\n    Person {\r\n      first_name,\r\n      last_name,\r\n      age,\r\n      weight,\r\n      height: input_height,\r\n    }\r\n  }\r\n}<\/pre>\n<p>As you can see <code>first_name<\/code>, <code>last_name<\/code>, <code>age<\/code> and <code>weight<\/code> are passed in as parameters whose values are then used directly in creating the <code>Person<\/code> struct. <code>input_height<\/code> does not match up and therefore still needs to be passed in manually. This cannot be done in Java so there is nothing to compare it to.<\/p>\n<p>Finally, we have made it to the end\u2026 That was a lot longer than I was expecting it to be.<\/p>\n<p>In conclusion Rust structs are like Java\u2019s objects, allowing us to group together data within a logical unit. Methods can then be invoked on the data held inside a struct instance but remember to add a <code>&amp;self<\/code> reference or it won\u2019t work. We also have associated functions that don\u2019t require an instance and can be used to construct a struct instance without access to it\u2019s private fields. We can also access data from a struct but we need to make sure we actually have access to the fields we are interested in.<\/p>\n<p>There is definitely a lot of stuff in this post and I appreciate that it probably took a while to go through it all. If you found this post helpful then maybe my previous Rust for Java Devs posts will also provide value. As always, please share this post if you liked it and follow me on twitter at <a href=\"https:\/\/twitter.com\/LankyDanDev\" target=\"_blank\" rel=\"noopener\">@LankyDanDev<\/a> to keep up with my new posts as I write them.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>Published on Web Code Geeks with permission by Dan Newton, partner at our <a href=\"\/\/www.webcodegeeks.com\/join-us\/wcg\/\" target=\"_blank\" rel=\"noopener\">WCG program<\/a>. See the original article here: <a href=\"https:\/\/lankydanblog.com\/2018\/03\/01\/rust-for-java-devs-structs\/\" target=\"_blank\" rel=\"noopener\">Rust for Java Devs \u2013 Structs<\/a><\/p>\n<p>Opinions expressed by Web Code Geeks contributors are their own.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Next up in Rust for Java Devs we have structs. They are used to hold data within a logical unit that can be passed to other functions or execute their own methods on the values that they store. Now this sounds very familiar\u2026 Java objects do the same thing. For example if you took a &hellip;<\/p>\n","protected":false},"author":2345,"featured_media":4128,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[77,366],"class_list":["post-21018","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby","tag-java","tag-rust"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Rust for Java Devs \u2013 Structs - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Next up in Rust for Java Devs we have structs. They are used to hold data within a logical unit that can be passed to other functions or execute their own\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Rust for Java Devs \u2013 Structs - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Next up in Rust for Java Devs we have structs. They are used to hold data within a logical unit that can be passed to other functions or execute their own\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-02T10:15:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/ruby-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Dan Newton\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@LankyDanDev\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dan Newton\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/\"},\"author\":{\"name\":\"Dan Newton\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/524f64635e723ac65532a14a6d85b762\"},\"headline\":\"Rust for Java Devs \u2013 Structs\",\"datePublished\":\"2018-03-02T10:15:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/\"},\"wordCount\":2228,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/ruby-logo.jpg\",\"keywords\":[\"Java\",\"Rust\"],\"articleSection\":[\"Ruby\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/\",\"name\":\"Rust for Java Devs \u2013 Structs - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/ruby-logo.jpg\",\"datePublished\":\"2018-03-02T10:15:07+00:00\",\"description\":\"Next up in Rust for Java Devs we have structs. They are used to hold data within a logical unit that can be passed to other functions or execute their own\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/ruby-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/ruby-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ruby\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/ruby\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Rust for Java Devs \u2013 Structs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/524f64635e723ac65532a14a6d85b762\",\"name\":\"Dan Newton\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b5024a6405bf07e14dacc299779fc0abded33b42a05190784776429e2c76435f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b5024a6405bf07e14dacc299779fc0abded33b42a05190784776429e2c76435f?s=96&d=mm&r=g\",\"caption\":\"Dan Newton\"},\"sameAs\":[\"https:\/\/lankydanblog.com\/\",\"https:\/\/www.linkedin.com\/in\/danknewton\/\",\"https:\/\/x.com\/LankyDanDev\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/dan-newton\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Rust for Java Devs \u2013 Structs - Web Code Geeks - 2026","description":"Next up in Rust for Java Devs we have structs. They are used to hold data within a logical unit that can be passed to other functions or execute their own","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/","og_locale":"en_US","og_type":"article","og_title":"Rust for Java Devs \u2013 Structs - Web Code Geeks - 2026","og_description":"Next up in Rust for Java Devs we have structs. They are used to hold data within a logical unit that can be passed to other functions or execute their own","og_url":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2018-03-02T10:15:07+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/ruby-logo.jpg","type":"image\/jpeg"}],"author":"Dan Newton","twitter_card":"summary_large_image","twitter_creator":"@LankyDanDev","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Dan Newton","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/"},"author":{"name":"Dan Newton","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/524f64635e723ac65532a14a6d85b762"},"headline":"Rust for Java Devs \u2013 Structs","datePublished":"2018-03-02T10:15:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/"},"wordCount":2228,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/ruby-logo.jpg","keywords":["Java","Rust"],"articleSection":["Ruby"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/","url":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/","name":"Rust for Java Devs \u2013 Structs - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/ruby-logo.jpg","datePublished":"2018-03-02T10:15:07+00:00","description":"Next up in Rust for Java Devs we have structs. They are used to hold data within a logical unit that can be passed to other functions or execute their own","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/ruby-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/ruby-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/ruby\/rust-java-devs-structs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Ruby","item":"https:\/\/www.webcodegeeks.com\/category\/ruby\/"},{"@type":"ListItem","position":3,"name":"Rust for Java Devs \u2013 Structs"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/524f64635e723ac65532a14a6d85b762","name":"Dan Newton","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b5024a6405bf07e14dacc299779fc0abded33b42a05190784776429e2c76435f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b5024a6405bf07e14dacc299779fc0abded33b42a05190784776429e2c76435f?s=96&d=mm&r=g","caption":"Dan Newton"},"sameAs":["https:\/\/lankydanblog.com\/","https:\/\/www.linkedin.com\/in\/danknewton\/","https:\/\/x.com\/LankyDanDev"],"url":"https:\/\/www.webcodegeeks.com\/author\/dan-newton\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/21018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/2345"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=21018"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/21018\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/4128"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=21018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=21018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=21018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}