In this lecture we will begin using the reg x crate we put in our dependencies earlier on.
So let's start out by just simply importing it in.
And now we want to create a new function called replace.
And it's going to accept three parameters.
Our first one is going to be our target, which is going to be a reference to a string slice.
And our target is going to be the pattern that we want to replace.
And then we're going to have our replace, which is also a string slice.
And that's going to be what we replace our target with.
And then we have our data, which is also a reference to a string slice, and our data is going to reference
the input, the text we read in from our input file.
So now we're going to return a result.
And in the event that the result is OC, we want to return it with a string.
Otherwise, if it's an air, we want to return a reg x air.
So now we're going to call regex new.
So we'll say let reg x equals reg x new.
And it's going to be done on our target, which is the string that we want to look for and this method
can fail.
So in the event that it does fail, we want to propagate it up, which we learned earlier on in this
course that that is done by.
Adding a question mark at the end.
So assuming that the method.
Successfully runs, then we want to return.
Okay.
And then we want to say reg x dot replace all.
Replace all, and then we want to replace all of the targeted strings inside of data with the string
that we want it to be replaced with.
And then we want to just say to string because that is how our result expects it to be returned.
So we are making sure it is explicitly going to be returned as a string and that's all we have to do
inside of that function.
So now we need to add this into our core logic.
So we're going to say let replace data and then we're going to match on replace.
And our target is our dot pattern.
Our replacement string is our replace.
And then our data is just a reference to the data we created right here.
And then inside of here.
Oop.
I need to make this as a reference.
And then inside of here, if it is okay, then we want that data to be given to us and assign to replace
data.
And then otherwise, if it's an air, well, then we're going to just kind of follow what we have been
doing.
So our first thing is going to be the red and bold error word, and then we're going to say fail to
replace text and then we want our error message to be printed out.
So now we're going to say air dot red, dot bold, and now our error message.
And then we want our process to exit with error code one.
And now for writing to the new file we want to write in our.
Replaced data.
And that should be all we have to do.
So now let's test this out.
So we're going to create a new file called Test Dot Text.
And then inside of here, we will say this is a test.
We'll save it.
And now we can run our program, say cargo run.
And we will say this is a test and that's what we want to replace and we want to replace it with
success.
And then our input file is test txt and then test modify.
It will be our output file.
So now let's run it and it says wrong number of arguments given.
Oh, I need to put these inside of quotations.
There we go.
So now let's check our test mod, our test mod, and we expect it to just say success.
And inside that is exactly what it says.
So we have successfully found and replaced strings.
But let's make sure that our air handling is correct as well.
So.
We'll look for an open and closed bracket and we'll just say inside of will replace it with test and
then we'll say test test and we'll say test.
So I know that this should fail, but let's make sure that it does and it does.
So we see that we have a reg x pass ere an enclosed character class.
And it failed right here.
So we see that our air was propagated up.
So now we have successfully handled our errors and created a find and replace command line program.
So.
We have successfully put together a lot of the main points we have learned about so far in this course.
So congratulations on the achievement of making it this far and understanding the logic behind what
we have been doing.
So let's continue our rest journey in the next section.