DEV Community

林子篆
林子篆

Posted on • Originally published at dannypsnl.github.io on

How to get started with Rust

Rust is a strange language.

Let’s start!

Moving

The first point is move semantic.

fn main() {
    let s = "hello".to_string();
    let t = s;
    let u = s;
}
Enter fullscreen mode Exit fullscreen mode

What do you expect? t & u is s? No!

rustc says:

error[E0382]: use of moved value: `s`
 --> main.rs:4:9
  |
3 | let t = s;
  | - value moved here
4 | let u = s;
  | ^ value used here after move
  |
  = note: move occurs because `s` has type `std::string::String`, which does not implement the `Copy` trait
Enter fullscreen mode Exit fullscreen mode

These errors tell you why you got a fail.

In Rust, you should expect default behavior of assign is moving!

Copy

And you can expect if any type implements the Copy trait, will be copied.

That’s why these code will work:

let s = 1;
let t = s;
let u = s;
Enter fullscreen mode Exit fullscreen mode

Implement the Copy trait can use derive notation in Rust.

#[derive(Clone, Copy)]
struct Foo {
    number: i32
}
Enter fullscreen mode Exit fullscreen mode

Notice that Clone is required.

But a type implement Copy can’t have a field do not implement Copy!!!

So following code will fail.

#[derive(Clone, Copy)]
struct Foo {
    label: String
}
Enter fullscreen mode Exit fullscreen mode

Error message:

error[E0204]: the trait `Copy` may not be implemented for this type
 --> main.rs:1:17
  |
1 | #[derive(Clone, Copy)]
  | ^^^^
2 | struct Foo {
3 | number: String,
  | -------------- this field does not implement `Copy`
Enter fullscreen mode Exit fullscreen mode

Mutable

In Rust, mutable and immutable is very different.

let s = "one ".to_string();
s.push_str("two ");
Enter fullscreen mode Exit fullscreen mode

Error report:

error[E0596]: cannot borrow immutable local variable `s` as mutable
 --> main.rs:3:5
  |
2 | let s = "one ".to_string();
  | - consider changing this to `mut s`
3 | s.push_str("two ");
  | ^ cannot borrow mutably
Enter fullscreen mode Exit fullscreen mode

Because String::push_str borrow &mut self, it can’t be used by an immutable String.

Reference

We already have seen String, it’s called owning pointer, others are Vec, Box, etc.

But these pointers will move data. Sometimes, we don’t need to move data, but also don’t want to copy it.

It’s reference showtime!

Rust’s reference has some points.

  1. explicitly using thing it points to
let a = 1;
let r = &a;
assert!(*r == 1);
Enter fullscreen mode Exit fullscreen mode
  1. mutable reference to mutable ownership
let mut num = 15;
let r = &mut num;
*r += 10;
assert!(*r == 25);
Enter fullscreen mode Exit fullscreen mode
  1. references are never null
  2. you can’t borrow a value will outlive when you still alive
let r;
{
    let x = 1;
    r = &x;
}
Enter fullscreen mode Exit fullscreen mode

Error message:

error[E0597]: `x` does not live long enough
 --> main.rs:5:14
  |
5 | r = &x;
  | ^ borrowed value does not live long enough
6 | }
  | - `x` dropped here while still borrowed
7 | }
  | - borrowed value needs to live until here
Enter fullscreen mode Exit fullscreen mode

The problem is r can be accessed after x already be dropped! That means a dangling pointer. Rust disallowed it.

Conclusion

I think these are the hardest part when you’re the beginner of Rust.

Because Rust chooses a new way to handle its memory, move default, checking live-time, sharing data by reference.

Understanding these things is most important to get familiar with Rust.

Hope you like it & can get some help from this.

References:

Programming Rust

  • Author: Jim Blandy & Jason Orendorff
  • ISBN: 978-1-491-92728-1

Top comments (0)