Author: Eiko

Time: 2025-11-01 16:10:33 - 2025-11-01 17:10:36 (UTC)

Rust Basics

Rust is a system programming language that focuses on:

  • memory safety

    This means that Rust prevents common programming errors related to memory management, such as null pointer dereferencing and buffer overflows, without needing a garbage collector.

  • type safety

    Rust has a Hindley-Milner type system that can catch many errors at compile time, reducing runtime errors. This is similar to functional languages like Haskell, OCaml, and ML.

    把类型想象成一种“防呆设计”的接口:不同类型的插头形状不同,如果弄错了插头,是插不进去的。这给程序员提供了额外的安全保障owo

  • low-level control & speed

It is popular for performance-critical applications and systems programming.

Hindley-Milner Type System

This type system supports features like:

  • Algebraic data types (enums and structs)

  • Pattern matching

  • Type inference

  • Type variables (called generics in Rust)

    (Which enables parametric polymorphism)

  • Optionally : type classes (called traits in Rust)

    (Which enables ad-hoc polymorphism)

Let’s Have Fun With Rust!

Install Rust:

visit Rustup and run the following command in your terminal (depending on your OS):

curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh

在windows下安装Rust

  1. Install rustup-init.exe

  2. Install Visual-Studio

  3. Check ‘C++ Desktop’

  4. Run rustup-init.exe

验证安装

If installation is successful, you can verify it by checking the version:

rustc --version

Create a new Rust project:

cargo new hello_rust
cd hello_rust

Install rust-analyzer:

  • VSCode: Install the “rust-analyzer” extension from the marketplace.

  • Other editors

For vim there are 6 options, I personally use coc with coc-rust-analyzer.

Write Rust And Learn

We shall explore:

  • Hello world

  • File read/write

  • Enigma machine

    喵喵给你留下了一段密文

    ENCLU UKWQG BRDFA UZLGK JXNTK OTWEL AEQFT IMFAW OTBCS

Homework : 解密喵喵的密文,已知 position = AAA, rings = BBB (or AAA)

rotorL = rotorI   of Enigma I , notch at 'Q' (16)
rotorM = rotorII  of Enigma I , notch at 'E' (4)
rotorR = rotorIII of Enigma I , notch at 'V' (21)

 I      EKMFLGDQVZNTOWYHXUSPAIBRCJ   Q   1930   Enigma I
 II     AJDKSIRUXBLHWTMCQGZNPYFVOE   E   1930   Enigma I
 III    BDFHJLCPRTXVZNYEIWGAKMUSQO   V   1930   Enigma I
 Reflector B	YRUHQSLDPXNGOKMIEBFZCWVJAT

decode this:
`ENCLU UKWQG BRDFA UZLGK JXNTK OTWEL AEQFT IMFAW OTBCS`