24 days from Node.js to Rust

preface

All languages have an entry level, and once that level is reached, the only way to continue to achieve mastery is through practice

Python is popular because of its low entry standards; JavaScript is slightly harder to get started because of asynchrony, whereas typed languages are harder because of the additional context considerations.

Rust’s learning curve is like a roller coaster. Every time you think you know it, you fall back into ignorance. So far, the reader has not been able to get started, and this article will continue to fill in the gaps

Language differences

Programming style

Rust’s programming style is slightly different from JavaScript. The names of functions, variables, and modules in Rust are underlined, such as time_in_millis; Constructs are humped, such as CpuModel; Constants are underlined in uppercase, such as GLOBAL_TIMEOUT

Parenthesis optionality

The following is more common

if (x > y) { / * * / }

while (x > y) { / * * / }
Copy the code

However, the following is preferred in Rust, with a warning from Linter

if x > y { / * * / }

while x > y { / * * / }
Copy the code

Expression return value

Almost all complete code segments return values, such as 4 * 2 returns 8, and if true {1} else {0} returns 1. This means that you can assign an expression to a variable or return a function, such as:

fn main() {
    let apples = 6;
    let message = if apples > 10 {
        "Lots of apples"
    } else if apples > 4 {
        "A few apples"
    } else {
        "Not many apples at all"
    };

    println!("{}", message) // prints "A few apples"
}
Copy the code

Note that there is no semicolon after the three strings above, you can try to add the result

unit type

If {} else if {} else {} else {} add a semicolon to the code to see what happens:

let message = if apples > 10 {
    "Lots of apples";  // ⬅ Notice the rogue semi-colon
} else if apples > 4 {
    "A few apples"
} else {
    "Not many apples at all"
};
Copy the code

Rust does not compile and gives a hint of “if and else have incompatible types.” The full output reads as follows:

error[E0308]: `if` and `else` have incompatible types
  --> crates/day-7/syntax/src/main.rs:13:12
   |
11 |        let message = if apples > 10 {
   |   ___________________-
12 |  |         "Lots of apples";
   |  |         -----------------
   |  |         |               |
   |  |         |               help: consider removing this semicolon
   |  |         expected because of this
13 |  |     } else if apples > 4 {
   |  |____________^
14 | ||         "A few apples"
15 | ||     } else {
16 | ||         "Not many apples at all"17 | | |}; | | | ^ | | | _____ | | |... `if` and `else` have incompatible types
   |        expected `()`, found `&str`
Copy the code

You will be prompted with “Expected (), found & STR.” and will be prompted to remove the semicolon, but what does the () in the message mean? () is called unit type, which essentially means “none”, and an expression ending with a semicolon returns “none”, also called unit type. If we add only the semicolon to the if {} part, this part will return “none”. The compiler expects each conditional branch to return the same type, so when we remove the semicolon, all conditional branches will return the same type.

When you see a () in an error message, it means you should add or remove a semicolon somewhere

The function returns implicitly

The last code to execute the function is returned as the value, without adding a return declaration:

fn add_numbers(left: i64, right: i64) - >i64 {
    left + right  // ⬅ note that the semicolon is not added
}
Copy the code

Is equivalent to the following code:

fn add_numbers(left: i64, right: i64) - >i64 {
    return left + right
}
Copy the code

An array of

Rust supports arrays. We haven’t talked much about arrays since there have been many articles on them, but you can read rust Book: Ch 4.3 and Rust By Example: Ch 2.3

The disadvantages of rust arrays compared to JavaScript arrays are that they must be fixed in length and initialized with an initial value for each position. The following code is problematic:

let mut numbers = [1.2.3.4.5];
numbers.push(7);
println!("{:? }", numbers);
Copy the code

If you want to get JavaScript like array functionality, you need Vec or VecDeque, where Vec can add and subtract new content at the end and VecDeque can add and subtract new content at the end

Arrays and iterators are covered exclusively in this series of articles, but what you need to know is that there is a simple macro that gives a Vec similar ability:

let mut numbers = vec![1.2.3.4.5];  // ⬅ Notice the vec! macro
numbers.push(7);
println!("{:? }", numbers);
Copy the code

conclusion

The Rust Book is very important for Rust learners. If you haven’t read it yet, do it as soon as possible, or you will run into a lot of trouble. If you have, reread it. Every time you hit a roadblock, reread it. You’ll discover something new each time

In the next article, we’ll take a look at the base types in Rust and expand on constructs

More and more

  • Rust tutorial (1) From NVM to Rust
  • Rust tutorial (2) from NPM to Cargo for the front end
  • Configure Visual Studio Code in Rust (3)
  • Rust tutorial (4) Hello World
  • Rust Tutorial for the front end (5) Borrowing & Ownership
  • Part 1 of Rust Tutorial (6) String
  • Rust Tutorial (7)
  • Rust Tutorial (8)
  • Rust Tutorial (9)
  • Rust tutorial for the front end (10) from Mixins to Traits
  • Rust Tutorial (11) Module for the front-end