I hope you enjoyed this assignment and the questions that it involved.
Each question was progressively getting more challenging than the previous one.
So in this video, I'm going to go over the solution to each question.
If you have any questions about my solution, please ask them in the Q&A section.
So question one States create three variables with the names Val.
One, Val to an answer.
So let's go ahead and do that.
Okay.
So now we have our three variables.
We want to perform a simple operation of generating the modulo four value one and value two.
And we state that we need to assign the answer to the ants variable so we know value one is going to
be value five and value two is two.
And then we say, let ants hold the answer to the modulo of vowel one to vowel two.
So I believe the answer to this is going to be one.
So let's print this out and verify that that is correct.
And we want to print out ants.
So now we do cargo run and we see the answer is one.
So we have question one complete.
Question two is create a vector and put the values two, four, six, eight and ten inside the vector.
So for us to do this, we can say let vec equals and then we can use the vector macro since we know
what values we want to put in and we will put in two, four, six, eight and ten.
So a very super easy way for us to create a vector.
And then once we have created our vector, we want to print out the current values.
So to print them out, we will use the print line macro,
and now we will print out the vector.
So let's make sure our vector printed out and we see we have two, four, six, eight and ten.
Now we want to remove the value ten, add the value 12, and then print the vector back out to confirm
our results.
So to print it back out, we will copy and paste.
And now we want to remove the value ten.
Well, we see ten is the last value added in.
So we can very easily remove this value by executing the pop method on it.
And now we can add the value 12 by using the push method.
And if we run, this cannot borrow as mutable.
So what we need to do in order to mutate is we a.k.a change the values inside of the vector variable.
We need to add the mutt key word to it.
So now if we run it, we see that we have two, four, six, eight and 12, which is the desired output.
So again, any time you want to mutate a variable or change it, you have to have the mute or mutt keyword
added to it.
For question three, we want to create a function called concatenate string.
So can cat string.
And then inside excuse me.
And our main function, we want to create a string variable.
So we will say let stir one equals a string from and we want the value.
Hello.
The function is going to take one argument that is of type string.
So in here we'll have our vowel that is a string and we are going to return a string.
So now we are returning a string and then inside the function we want to contact the string world to.
Hello.
So what we want to do.
It's a vow.
Plus.
World.
And now if we come back to Maine and we say, let ants equal.
You can catch a string of stir one.
And now if we print out ants again, we see that we successfully concatenated with hello world.
And in case you might be wondering how come we were able to use ants down here and up here because we
re initialized ants here.
So the value up here is no longer valid.
Ants will now only be referred to by our value down here.
So now we have successfully created a string function that will concatenate strings together.
And then our last question is create a function called control flow.
So we have control flow.
So we created our function.
And this is going to take one argument that is an integer.
So we will say VAO and we'll just give it IE 32 and then based on this integer, print out the following
the value is one.
The value is greater than 50, the value is less than 25 or the value is greater than 25, but less
than 50.
Okay.
So inside of here, we want to check if Val is equal to one, then print out
the value is one.
And we're going to have a couple of print statements.
So let's go ahead and do this.
ELTs, if Val is greater than 50, print out.
The value is greater than 50.
Else if the val is.
Less than 25.
We want to print out
the value is less than 25.
Else.
Print out the value is greater than 25, but less than 50.
So now if we call this function control flow, let's pass in the value five and let's see what is returned.
The value is less than 25.
That is correct.
If we pass in 500.
The value is greater than 50.
That is correct.
If we pass in one.
Value is one.
That is correct.
If we pass in 25, the value is greater than 25, but less than 50.
And that is also correct, because we did not use an equal sign here.
Or here.
So now if we run this, we will see.
Value is less than 25 because now we are also including the value 25.
So hopefully you were able to understand and accomplish these questions with ease.
And if you were, then you are definitely ready to move on to the next section where we begin talking
about ownership.