Rust learning summary – write rust function, free raspberry pie

Time:2021-2-17

Updated at 16:53:50, September 8, 2020
The domestic image of SSVM has been released. When the first build of users in mainland China fails, they will automatically transfer to the domestic image the second time.


Learn from rust and get raspberry pie for free

This time we learn rust from 0, and we haven’t tried virtual machine before, so we lose our hair in the learning process. Let’s start without much nonsense

#Three questions of rust

What is rust?
Rust is a systems programming language, which takes into account safety, speed and concurrency.

What can rust do?
As a low-level system programming language, rust can be used in all fields using C / C + +, such as embedded programming that requires fine control of hardware and application software that requires high performance (database engine, browser engine, 3D rendering engine, etc.).

What are the advantages of rust and other similar languages?
Compared with the systematic defect of C / C + + (security vulnerability caused by improper memory management), rust ensures memory security during compilation through ownership mechanism, and does not need garbage collection (GC) or manual memory release.

#The overall idea of building rust environment

  • Method a:
    If it is win7 / 8, you can deploy the virtual machine, and then install Ubuntu server 20.04 TLS and rust. If it is win10, you can choose to install Ubuntu server 20.04 TLS in the application market.
  • Method B:
    Using docker

Here I use the Ubuntu virtual machine to build rust.

#Building rust in virtual machine Ubuntu environment

# Install Rust
#Since we need to use rust to write functions, we also need to install the compiler and tools of rust.
#If you think it's slow, you can use the image of China University of science and technology. Please search by yourself
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env

# Install Node.js
$ curl -sL https://deb.nodesource.com/setup_14.x |  bash
$ apt-get install -y nodejs

# Install ssvmup toolchain
#Finally, the ssvmup tool automatically executes the build process and generates all artifacts, making it easy for node applications to call the rust function. Again, you need to install the ssvmup dependency.
$ npm install -g ssvmup # Append --unsafe-perm if permission denied

# OS dependencies for SSVM
$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo apt install build-essential curl wget git vim libboost-all-dev

# Install the nodejs addon for SSVM
$ npm install ssvm

#The first rust program

fn main() {
  let txt = "Hello, world!";
  println!("I say : {}", txt);
}

usefnDeclaring functions, andJavaScriptIt’s quite different.main()yesRustThe default entry to the program,println!Represents printing text to the console.

RustThere are two ways to output textPrintln!) and print!). These two “functions” are methods of outputting strings to the command line. The only difference is that the former outputs a new line character at the end of the output.

#What did I do

Made a multiplication table, ha ha ha! Final code:GitHub

Rust learning summary - write rust function, free raspberry pie

#Problems encountered

Q: Win10 Linux subsystem runs the command trust init too slowly
Add the rustup in the script_ UPDATE_ Root variableThe mirror image of University of science and technology of China

#University of science and technology of China
RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

Q: Win10 Linux subsystem runs the command “trust init”, indicating an exception

thread 'main' panicked at 'assertion failed: `(left == right)
...
thread panicked while panicking. aborting.
Illegal instruction (core dumped)

Discussion on this exception:

resolvent:

  • use older linux versions
    On August 24, 2020, there is only Ubuntu 20.04 in the Microsoft Store in mainland China, and the version reduced does not match the version recommended by this project.
  • some users report that exporting RUSTUP_IO_THREADS=1 mitigates the problem
    With this command, no exception will be prompted, but it will be executed againcurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shThere will be no information or progress
  • use WSLv2
    On August 24, 2020, I failed. My window 10 version number is 1909, and my operating system version is 18363.1016
    Minimum operating system version required18917For the update method, seeHow to install windows 10 wslv2 manually
    More detailed installation / upgrade can be found in the official Microsoft documentation——Windows Linux subsystem windows 10 installation guide

Q: Win10 – your host does not meet the minimum requirements for running vmware workstation with Hyper-V or device / credential guard enabled.
terms of settlement

Q:Ubuntu —— sudo npm install -g ssvmupStrange hint after

> [email protected] preuninstall /usr/local/lib/node_modules/ssvmup
> node ./uninstall.js

When this prompt appears, execute the following command and waitssvmup has been installed!Then:

$ cd /usr/local/lib/node_modules/ssvmup
$ node install.js 

Q: Ubuntu – ssvmup is added through NPM install – G ssvmup, but when ssvmup buil is executed, the system prompts:You have not installed ssvmup
In fact, I need to run it in the directory where the rust file is located. Finally, I will install ssvmup in the global, and at the same time, I will install ssvmup in a single project, and run the ssvmup command through NPM srcipt

Q:Ubuntu —— ssvmup buildfail

[INFO]: Installing wasm-bindgen...
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { description: "Couldn\'t connect to server", code: 7, extra: None }', src/install/mod.rs:182:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Well, this is embarrassing.

terms of settlement:secondstate.ioTo sum up, we need to go online scientifically (according to the official feedback, a domestic image will be released in the near future)

#Summary

Rust is a young and potential programming language.

#Reference