Part 1: Today we learn Rust
I started working with the programming language Rust about 1 week ago. When I started programming again a year ago, I had shortlisted the languages to Go and Rust. Ultimately I decided to lear Go.
The reason why I’m stopping by Rust after a year is pure curiosity. I’ve heard a lot of good things about the language and it doesn’t make you dumber either 😇.
should be feasible
I decided to do the whole thing as part of the challenge. Programming 1 hour a day is not that difficult for me now, I usually do that anyway. It remains to be seen whether I can consistently hold out fo.
Day 1 - Rust, Methods and Ownership
Like I said, I started Rust a week ago. I.e. I read the book (great to read by the way) and did the Rustlings course. Today I started to rewrite a small program in Rust. I learned a few things about ownership in Rust. I think this is best illustrated by the code:
|
|
That looks right at first glance. If you let the program run you will get an error:
|
|
In Go you would initialize the person once and then use the methods as often as you like. Go would only differentiate between a pointer and a non-pointer receiver, but the function could still be used.
In Rust, using the method consumes the object, since it owns the object by using self.
Since I would like to use the person after using the method, I have to borrow this to the method:
|
|
And everything works. After finishing the code block in the hello method, self is returned.
It will probably take me a little while to get used to it and stop at it one or two times.