<buttonid="sidebar-toggle"class="icon-button"type="button"title="Toggle Table of Contents"aria-label="Toggle Table of Contents"aria-controls="sidebar">
<inputtype="search"id="searchbar"name="searchbar"placeholder="Search this book ..."aria-controls="searchresults-outer"aria-describedby="searchresults-header">
<p><strong>See this chapter on YouTube: <ahref="https://youtu.be/W23uQghBOFk">Part 1</a> and <ahref="https://youtu.be/GSVhrjLCuNA">Part 2</a></strong></p>
<p>With structs, you can create your own type. You will use structs all the time in Rust because they are so convenient. Structs are created with the keyword <code>struct</code>. The name of a struct should be in UpperCamelCase (capital letter for each word, no spaces). If you write a struct in all lowercase, the compiler will tell you.</p>
<p>There are three types of structs. One is a "unit struct". Unit means "doesn't have anything". For a unit struct, you just write the name and a semicolon.</p>
<p>The next is a tuple struct, or an unnamed struct. It is "unnamed" because you only need to write the types, not the field names. Tuple structs are good when you need a simple struct and don't need to remember names.</p>
let my_colour = Colour(50, 0, 50); // Make a colour out of RGB (red, green, blue)
println!("The second part of the colour is: {}", my_colour.1);
}
</code></pre></pre>
<p>This prints <code>The second part of the colour is: 0</code>.</p>
<p>The third type is the named struct. This is probably the most common struct. In this struct you declare field names and types inside a <code>{}</code> code block. Note that you don't write a semicolon after a named struct, because there is a whole code block after it.</p>
<pre><preclass="playground"><codeclass="language-rust">struct Colour(u8, u8, u8); // Declare the same Colour tuple struct
struct SizeAndColour {
size: u32,
colour: Colour, // And we put it in our new named struct
}
fn main() {
let my_colour = Colour(50, 0, 50);
let size_and_colour = SizeAndColour {
size: 150,
colour: my_colour
};
}
</code></pre></pre>
<p>You separate fields by commas in a named struct too. For the last field you can add a comma or not - it's up to you. <code>SizeAndColour</code> had a comma after <code>colour</code>:</p>
<pre><preclass="playground"><codeclass="language-rust">struct Colour(u8, u8, u8); // Declare the same Colour tuple struct
struct SizeAndColour {
size: u32,
colour: Colour, // And we put it in our new named struct
}
fn main() {}
</code></pre></pre>
<p>but you don't need it. But it can be a good idea to always put a comma, because sometimes you will change the order of the fields:</p>
<pre><preclass="playground"><codeclass="language-rust">struct Colour(u8, u8, u8); // Declare the same Colour tuple struct
colour: Colour // ⚠️ Whoops! Now this doesn't have a comma.
size: u32,
}
fn main() {}
</code></pre></pre>
<p>But it is not very important either way so you can choose whether to use a comma or not.</p>
<p>Let's create a <code>Country</code> struct to give an example. The <code>Country</code> struct has the fields <code>population</code>, <code>capital</code>, and <code>leader_name</code>.</p>
<pre><preclass="playground"><codeclass="language-rust">struct Country {
population: u32,
capital: String,
leader_name: String
}
fn main() {
let population = 500_000;
let capital = String::from("Elista");
let leader_name = String::from("Batu Khasikov");
let kalmykia = Country {
population: population,
capital: capital,
leader_name: leader_name,
};
}
</code></pre></pre>
<p>Did you notice that we wrote the same thing twice? We wrote <code>population: population</code>, <code>capital: capital</code>, and <code>leader_name: leader_name</code>. Actually, you don't need to do that. If the field name and variable name are the same, you don't have to write it twice.</p>
<pre><preclass="playground"><codeclass="language-rust">struct Country {
population: u32,
capital: String,
leader_name: String
}
fn main() {
let population = 500_000;
let capital = String::from("Elista");
let leader_name = String::from("Batu Khasikov");