- So we've seen how to declare structure types.
What we're going to look at next
is how to create this structure instance
and to access its fields.
So to create a structure instance,
you use the name of the structure like employee,
followed by curly brackets.
And inside the curly brackets,
you initialize all the fields,
the name, the salary, et cetera, in any order.
Here's an example.
I've created an employee instance or an object,
and inside the curly brackets,
I've initialized the name to be a string Jane,
salary 1,000 and a full-time status as true.
Okay, so an important point about structures
is that they're allocated on the stack.
They're not allocated on the heap.
There's no dynamic memory being involved.
There's no garbage collection.
Structure instances, when you create them,
I guess they're just allocated on the stack
which means at the end of the function,
they get popped out the stack and they disappear.
Okay, so the memory model in Rust
is quite simple really compared to other languages.
Structures get allocated on the stack and their lifetime,
if you create them this way,
is bounded by the function in which they're defined.
When they go out of scope, they get dropped.
Right, well, so that's a structure instance.
What if you want to have a mutable structure instance,
one where you could change the fields?
Well, you just use the mut keyword
in the variable declaration.
Here's an example.
e2 is a mutable object, it's an employee.
Its name is John and its initial salary is 1,000.
And John is a not a full-time employee.
Okay, so the employee is mutable.
Notice that mutability applies to the whole object.
When you declare a mutable employee,
all of its fields are mutable,
the name, the salary, and the full-time status.
You can't define mutability on a field-by-field basis.
The whole structure object is either mutable or not.
Right, what about accessing fields in the structure?
Well, no surprise, use a dot.
So let's say I've created an employee object, e1,
and I've given it some initialization.
You can imagine, can't you?
You say e1.name, e1.salary, e1.fulltime,
okay, assuming that you've got access to those fields
if they're in the same module and if they're public,
so straightforward enough,
if the structure variable is declared as mutable,
then of course you could change the fields if you wanted to.
So e2 is a mutable employee
and I've doubled the employee salary,
a good day in the office,
and then I've output the employee's name, the new salary,
and the full-time status at the end.
So quite straightforward, no great surprises there.
Let's have a look at the example in the project,
lesson10_structs_getting_started,
the files we're gonna look at, well, mytypes.rs,
it's the same employee structure type as we saw previously.
Main.rs, it declares that module
and runs the code for this part of the demo,
the code for this part of the demo, demo_instances.
Okay, so we'll take a look at the code
and then we'll run the project, let's see.
Okay, so quick reminder, here's mytypes.rs,
a public structure with public fields.
Remember, both need to be public.
So if you just declare the structure as public,
but not the fields, then these fields would be private
to this file, which isn't really what we wanted to retrieve.
We want them to be accessible elsewhere so they're public.
In main, I import, I declare the module,
plus all the different code modules as well.
Demo_instances is the module
for this particular part of the chapter.
So here is the call, let me uncomment demo_instances,
and it's here.
So I've got an example that creates immutable instance
and I've got a demo exception
that creates a mutable instance.
So here is an immutable employee
whose name and salary and full-time status are fixed
and here is a mutable employee.
So the whole of the object is mutable.
I choose to change the salary,
I could have changed any field that I like
and then I'll output the details there.
Right, again, that's quite straightforward, I think,
really let's just give that a run
just to make sure that it all works fine.
Cargo run.
Okay, so Jane was the initial employee
with a fixed salary of 1,000 and full-time was true.
And then John, which was mutable,
had a double salary and his full-time status false.
Okay, so that's quite straightforward.
No great surprises so far.
All good so far.