90 lines
2.7 KiB
Markdown
90 lines
2.7 KiB
Markdown
---
|
|
title: "Part 1: Today we learn Rust"
|
|
date: 2020-09-18
|
|
draft: false
|
|
series: ["Starting with rust"]
|
|
tags: ["rust","coding"]
|
|
---
|
|
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 [](https://www.100daysofcode.com/) 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:
|
|
|
|
```rust
|
|
use std::string::String;
|
|
|
|
#[derive(Debug)]
|
|
struct Person {
|
|
name: String,
|
|
age: i32,
|
|
}
|
|
|
|
impl Person {
|
|
fn hello(self) {
|
|
println!("Hello {}, your age is {}", self.name, self.age);
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let bob = Person {
|
|
name: String::from("Bob"),
|
|
age: 32,
|
|
};
|
|
bob.hello();
|
|
println!("{:?}", bob)
|
|
}
|
|
```
|
|
That looks right at first glance. If you let the program run you will get an error:
|
|
|
|
```rust
|
|
error[E0382]: borrow of moved value: `bob`
|
|
--> src/main.rs:21:22
|
|
|
|
|
16 | let bob = Person {
|
|
| --- move occurs because `bob` has type `Person`, which does not implement the `Copy` trait
|
|
...
|
|
20 | bob.hello();
|
|
| ------- `bob` moved due to this method call
|
|
21 | println!("{:?}", bob)
|
|
| ^^^ value borrowed here after move
|
|
|
|
|
note: this function consumes the receiver `self` by taking ownership of it, which moves `bob`
|
|
--> src/main.rs:10:14
|
|
|
|
|
10 | fn hello(self) {
|
|
|
|
|
^^^^
|
|
```
|
|
|
|
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:
|
|
|
|
```rust
|
|
|
|
// ...
|
|
|
|
impl Person {
|
|
fn hello(&self) {
|
|
println!("Hello" {}, your age is {}", self.name, self.age");
|
|
}
|
|
}
|
|
|
|
//...
|
|
```
|
|
|
|
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.
|