24 days from Node.js to Rust

preface

If you’ve never worked with a binary compiled language before, Rust doesn’t have much trouble working with it. Rust provides excellent support for compiling executables in different environments, making porting applications much easier

Hello world

Create a project

First we create a Rust project with Cargo New:

cargo new rust-test
Copy the code

By default, cargo New creates a template for binary applications, which is what we need now, but note that you can also create a library template using the cargo New –lib command

After executing the above command, you get a project like this:

Rust - test / ├ ─ ─. Git ├ ─ ─ the gitignore ├ ─ ─ Cargo. Toml └ ─ ─ the SRC └ ─ ─ main. RsCopy the code

The Rust version we use is 2021. If your Cargo. Toml file has edition as 2018, be aware that this article is based on 2021.

Package & Run

Now we can try running the project with cargo run:

$cargo run Compiling rust-test v0.1.0 (\rust-test) Finished dev [unoptimized + debuginfo] target(s)in1.28s Running 'target' \debug\rust-test.exe 'Hello, world!Copy the code

Cargo Run will call Cargo Build to pack your app, and then run the default (optionally specified) binary./target/debug/my-app will generate the binary after running this command. If you just want to pack and not run, you can execute the cargo build command. By default debug information is generated about file size, performance, etc. When you want to get the release code, you can run cargo Build –release and the binary is generated in the./target/release/rust-test directory

Source code analysis

Let’s take a look at the SRC /main.rs file in the template

fn main() {
  println!("Hello, world!");
}
Copy the code

The main() function is indispensable in a stand-alone executable and is the entry point to the program. println! () is a macro that prints arguments to STDOUT, more about macros later. Hello, world!” It’s a string, which is rust, because strings are going to be the first thing that gets in the way of learning rust, so let’s take a look

String problem 1

First we’ll start with “Hello, world!” Assign to a let-modified variable (let and const are used as keywords in Rust, similar to JavaScript. However, if you use const scenarios in JavaScript, use let for most rust scenarios.)

fn main() {
  let greeting = "Hello, world!";
  println!(greeting);
}
Copy the code

At this point, if your VS Code is configured as described in the previous article, you will already see errors in VS Code

Ignore this and try cargo Run:

$cargo run Compiling rust-test V0.1.0 (rust-test) error: format argument must be a string literal --> src\main.rs:3:14 | 3 | println! (greeting); | ^ ^ ^ ^ ^ ^ ^ ^ |help: you might be missing a string literal to format with | 3 | println! ("{}", greeting);
  |              +++++

error: could not compile `rust-test` due to previous error
Copy the code

If you want the above code to work, you are a “normal” programmer, because in many languages it works, strings are strings. But not in rust’s world. When writing rust code, get used to looking for error messages that not only tell you what the problem is, but also how to fix it. println! The first argument to () needs to be a string and supports variable formatting, with the following changes to the code:

fn main() { let greeting = "Hello, world!" ;+  println!("{}", greeting);
}
Copy the code

Cargo Run is executed again, now running correctly

String problem 2

Experienced programmers who want to abstract code for reuse will probably start with something like this:

fn main() {
  greet("World");
}

fn greet(target: String) {
  println!("Hello, {}", target);
}
Copy the code

From past programming experience, the above code should be fine, but when executed, the problem will be found:

$cargo run the Compiling rust - test v0.1.0 (rust - test) error [E0308] : mismatched types - > SRC \ main rs: 2:11 | 2 | greet ("World"); | ^ ^ ^ ^ ^ ^ ^ -help: try using a conversion method: `.to_string()`
  |           |
  |           expected struct `String`, found `&str`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rust-test` due to previous error
Copy the code

Follow ruSTC — Explain E0308 for more information, we can fix it as follows:

fn main() {
+ greet("World".to_string());} fn greet(target: String) { println! ("Hello, {}", target); }Copy the code

See String vs STR in Rust for details

conclusion

In Rust, you need to keep your mind alert to strings. In the following part, we will introduce the concept of ownership, which can help you solve some doubts about strings in this paper

Related links

  • Rust Tutorial for the front End (1) : From NVM to Rust
  • Rust Tutorial (2) for the front-end: FROM NPM to Cargo
  • Rust Tutorial (3) : Configuring Visual Studio Code
  • Rust tutorial (4) Hello World