[教學] Rust Programming

本帖最後由 javacomhk 於 2022-8-4 23:36 編輯

(1) rust compiler 安裝方法 Mac / Linux
  1. curl https://sh.rustup.rs -sSf | sh
複製代碼
  1. source "$HOME/.cargo/env"
  2. rustc -V
  3. cargo -V
複製代碼
(2) 測試寫 hello_world
  1. cargo new --bin hello_world
  2. cd hello_world
  3. cargo run
複製代碼
(3) 測試寫 game project tetris
安裝 dependencies (mac)
  1. brew install sdl2
  2. brew install sdl2_ttf
複製代碼
安裝 dependencies (apt package mananger)
  1. sudo apt-get install libsdl2-dev
  2. sudo apt-get install libsdl2-ttf-dev
複製代碼
下載 example project
https://github.com/PacktPublishi ... game_mechanisms.zip

解壓後
unzip game_mechanisms.zip
cd game_mechanisms
cargo run

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

第二部份 Cross Compile Rust Program Using Docker

(1) 在 Windows 下安裝及運行 Docker Desktop for Windows (https://docs.docker.com/desktop/install/windows-install/)

(2) 下載測試遊戲源代碼於 https://github.com/C14L/termsnake/archive/refs/heads/master.zip

(3) 解壓master.zip 於例如 C:\Users\user\Downloads

(4) 創建 Dockerfile 檔案於 例如 C:\Users\user\Downloads 如下內容
  1. FROM rust:latest

  2. RUN apt update && apt upgrade -y
  3. RUN apt install -y g++-mingw-w64-x86-64

  4. RUN rustup target add x86_64-pc-windows-gnu
  5. RUN rustup toolchain install stable-x86_64-pc-windows-gnu

  6. WORKDIR /app

  7. CMD ["cargo", "build", "--target", "x86_64-pc-windows-gnu"]
複製代碼
(5) 使用 command prompt  建立 Docker Image
  1. cd C:\Users\user\Downloads
  2. docker build . -t rust_cross_compile/windows
複製代碼
(6) 建立 Docker Image後 compile rust 程式
  1. cd C:\Users\user\Downloads\master\termsnake-master
  2. docker run --rm -v C:\Users\user\Downloads\master\termsnake-master:/app rust_cross_compile/windows
複製代碼
(7) compile 成功後, 試運行
  1. cd C:\Users\user\Downloads\master\termsnake-master
  2. cd target\x86_64-pc-windows-gnu\debug
  3. termsnake.exe
複製代碼

TOP

使用 Windows WSL2 及 Docker Desktop for Windows cross compile rust program

(1) 安裝 Windows WSL2 Ubuntu 及 Docker Desktop for Windows

(2) 在 Windows WSL2 安裝 rust compiler
  1. sudo apt update
  2. sudo apt install curl zip unzip
  3. curl https://sh.rustup.rs -sSf | sh
  4. source "$HOME/.cargo/env"
  5. rustc -V
  6. cargo -V
複製代碼
(3) 在 cargo 安裝 cross
  1. cargo install -f cross
複製代碼
(4) 下載測試遊戲源代碼
  1. curl -OL https://github.com/C14L/termsnake/archive/refs/heads/master.zip
  2. unzip master.zip
  3. cd termsnake-master/
  4. cross build --target x86_64-pc-windows-gnu
複製代碼

TOP

本帖最後由 javacomhk 於 2022-8-5 04:00 編輯

cross 支持很多 platforms (主要是為了 embedded system) https://github.com/cross-rs/cross
包括例如 Raspberry Pi
  1. # Cross-compiling to aarch64 (arm64)
  2. cross build --target aarch64-unknown-linux-gnu
  3. # Cross-compiling to armv7
  4. cross build --target armv7-unknown-linux-gnueabihf
複製代碼

TOP

本帖最後由 javacomhk 於 2022-9-20 01:00 編輯

第三部份,使用 Windows WSL2 Ubuntu 及 Visual Studio Code 進行開發,debug 及 build

(1) 安裝 Windows WSL2 Ubuntu 後 , 根據上面步驟,安裝 rust 及 Docker Desktop for Windows

(2) 下載及安裝 Visual Studio Code for Windows https://code.visualstudio.com/download

(3) 在  Visual Studio Code 安裝遠端開發擴充套件 Remote - WSL extensions

(4) 在  Visual Studio Code 安裝其他 extensions 包括 rust-analyzer 及 codeLLDB

(5) 測試及 debug hello_world
  1. cargo new --bin hello_world
  2. cd hello_world
  3. code .
複製代碼
(6) 在  Visual Studio Code 設立 breakpoint 後 F5 Start Debugging,參考


(7) git clone 一些 rust 項目 在  Visual Studio Code edit 及 debug
  1. git clone https://github.com/uutils/coreutils.git
  2. code .
複製代碼
(8) 構建 x86_64-pc-windows-gnu exe 檔案
開啟 Docker Desktop for Windows
在 WSL2 Ubuntu 內  cross build
  1. cd coreutils
  2. cross build --target x86_64-pc-windows-gnu --release --features windows
  3. cp target/x86_64-pc-windows-gnu/release/coreutils.exe /mnt/c/Users/user/.
複製代碼
在  command prompt 下測試
  1. C:\Users\user\coreutils.exe date -I minutes
複製代碼
其他 rust 改寫嘅 utilities

https://betterprogramming.pub/5- ... -tools-506af07b6d54

TOP

本帖最後由 javacomhk 於 2022-8-7 08:12 編輯

第四部份, Rust and WebAssembly

Youtube 教學


下載 project code 及解壓
  1. curl -OL https://github.com/yishn/lets-code/archive/refs/heads/main.zip
  2. unzip main.zip
  3. cd lets-code-main/minesweeper
複製代碼
安裝 wasm-pack  及 build
  1. curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
  2. wasm-pack build --target web
複製代碼
安裝 nodejs 及 npm
  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

  2. #Append the following lines to ~/.bashrc :
  3. export NVM_DIR="$HOME/.nvm"
  4. [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

  5. nvm install 16
複製代碼
安裝 及 啟動 web server
  1. npm install serve -g
  2. serve
複製代碼
在 Browser 測試 Web server
- Local:            http://localhost:3000

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

TOP