Rust, Zero to Hello World

Fast and inexpensive to learn, but beware the dependencies

2023/02

Rust with Actix

I installed Rust using https://rustup.rs/ and created a project with Cargo using The Rust Programming Language.

$ cargo --version
-> cargo 1.67.1 (8ecd4f20a 2023-01-10)

$ cargo new rust-test
$ cd rust-test

Then perused the Actix crate docs and https://actix.rs/.

# actix-web, not actix
$ cargo add actix-web

Project Tree

├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs

I replaced main.rs with the Hello World! example at https://actix.rs/.

// main.rs
use actix_web::{get, web, App, HttpServer, Responder};

#[get("/")]
async fn index() -> impl Responder {
    "Hello, World!"
}

#[get("/{name}")]
async fn hello(name: web::Path<String>) -> impl Responder {
    format!("Hello {}!", &name)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(index).service(hello))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}
$ cargo build
$ cargo run

$ curl localhost:8080
-> Hello, World!

Executable size

rust-test - debug   - 104MB
rust-test - release - 9.3MB

From start to finish, including these notes, was about 20 minutes. I accidentally added actix instead of actix-web, which slowed me down a little. My main concern with Rust is that if feels like Node.js and npm. It’s super easy to type cargo add actix-web and go, but then it added so many dependencies. The Cargo.lock file is 1176 lines long.

Here is the output from the build command that shows the dependencies.

# debug mode
$ cargo build
->Downloaded actix-codec v0.5.0
  Downloaded http v0.2.9
  Downloaded semver v1.0.16
  Downloaded convert_case v0.4.0
  Downloaded local-waker v0.1.3
  Downloaded unicode-bidi v0.3.10
  Downloaded base64 v0.21.0
  Downloaded ahash v0.7.6
  Downloaded alloc-no-stdlib v2.0.4
  Downloaded zstd v0.12.3+zstd.1.5.2
  Downloaded zstd-safe v6.0.4+zstd.1.5.4
  Downloaded actix-server v2.2.0
  Downloaded actix-router v0.5.1
  Downloaded time-macros v0.2.7
  Downloaded actix-http v3.3.0
  Downloaded alloc-stdlib v0.2.2
  Downloaded actix-web-codegen v4.1.0
  Downloaded actix-web v4.3.0
  Downloaded paste v1.0.11
  Downloaded rustc_version v0.4.0
  Downloaded regex v1.7.1
  Downloaded cookie v0.16.2
  Downloaded encoding_rs v0.8.32
  Downloaded zstd-sys v2.0.7+zstd.1.5.4
  Downloaded brotli v3.3.4
  Downloaded brotli-decompressor v2.3.4
  Downloaded derive_more v0.99.17
  Downloaded actix-utils v3.0.1
  Downloaded actix-service v2.0.2
  Downloaded actix-macros v0.2.3
  Downloaded tinyvec_macros v0.1.1
  Downloaded time v0.3.19
  Downloaded serde_json v1.0.93
  Downloaded language-tags v0.3.2
  Downloaded local-channel v0.1.3
  Downloaded bytestring v1.2.0
  Downloaded jobserver v0.1.25
  Downloaded 37 crates (5.3 MB) in 0.44s (largest was `encoding_rs` at 1.4 MB)
   Compiling libc v0.2.139
   Compiling cfg-if v1.0.0
   Compiling autocfg v1.1.0
   Compiling pin-project-lite v0.2.9
   Compiling log v0.4.17
   Compiling version_check v0.9.4
   Compiling memchr v2.5.0
   Compiling bytes v1.4.0
   Compiling futures-core v0.3.26
   Compiling itoa v1.0.5
   Compiling parking_lot_core v0.9.7
   Compiling scopeguard v1.1.0
   Compiling smallvec v1.10.0
   Compiling once_cell v1.17.1
   Compiling typenum v1.16.0
   Compiling proc-macro2 v1.0.51
   Compiling serde v1.0.152
   Compiling pkg-config v0.3.26
   Compiling unicode-ident v1.0.6
   Compiling quote v1.0.23
   Compiling tracing-core v0.1.30
   Compiling futures-task v0.3.26
   Compiling futures-util v0.3.26
   Compiling fnv v1.0.7
   Compiling syn v1.0.107
   Compiling http v0.2.9
   Compiling pin-utils v0.1.0
   Compiling percent-encoding v2.2.0
   Compiling generic-array v0.14.6
   Compiling lock_api v0.4.9
   Compiling tokio v1.25.0
   Compiling tracing v0.1.37
   Compiling futures-sink v0.3.26
   Compiling paste v1.0.11
   Compiling indexmap v1.9.2
   Compiling aho-corasick v0.7.20
   Compiling slab v0.4.7
   Compiling bytestring v1.2.0
   Compiling jobserver v0.1.25
   Compiling signal-hook-registry v1.4.1
   Compiling mio v0.8.6
   Compiling socket2 v0.4.7
   Compiling getrandom v0.2.8
   Compiling crc32fast v1.3.2
   Compiling tinyvec_macros v0.1.1
   Compiling parking_lot v0.12.1
   Compiling cc v1.0.79
   Compiling zstd-safe v6.0.4+zstd.1.5.4
   Compiling local-waker v0.1.3
   Compiling regex-syntax v0.6.28
   Compiling alloc-no-stdlib v2.0.4
   Compiling rand_core v0.6.4
   Compiling tinyvec v1.6.0
   Compiling ahash v0.7.6
   Compiling alloc-stdlib v0.2.2
   Compiling hashbrown v0.12.3
   Compiling block-buffer v0.10.3
   Compiling crypto-common v0.1.6
   Compiling time-core v0.1.0
   Compiling adler v1.0.2
   Compiling httparse v1.8.0
   Compiling ppv-lite86 v0.2.17
   Compiling digest v0.10.6
   Compiling miniz_oxide v0.6.2
   Compiling time-macros v0.2.7
   Compiling brotli-decompressor v2.3.4
   Compiling rand_chacha v0.3.1
   Compiling unicode-normalization v0.1.22
   Compiling actix-service v2.0.2
   Compiling actix-utils v3.0.1
   Compiling form_urlencoded v1.1.0
   Compiling cookie v0.16.2
   Compiling unicode-bidi v0.3.10
   Compiling bitflags v1.3.2
   Compiling cpufeatures v0.2.5
   Compiling serde_json v1.0.93
   Compiling convert_case v0.4.0
   Compiling ryu v1.0.12
   Compiling sha1 v0.10.5
   Compiling flate2 v1.0.25
   Compiling zstd-sys v2.0.7+zstd.1.5.4
   Compiling rand v0.8.5
   Compiling idna v0.3.0
   Compiling local-channel v0.1.3
   Compiling regex v1.7.1
   Compiling brotli v3.3.4
   Compiling num_cpus v1.15.0
   Compiling encoding_rs v0.8.32
   Compiling base64 v0.21.0
   Compiling language-tags v0.3.2
   Compiling httpdate v1.0.2
   Compiling mime v0.3.16
   Compiling url v2.3.1
   Compiling serde_urlencoded v0.7.1
   Compiling actix-router v0.5.1
   Compiling time v0.3.19
   Compiling tokio-util v0.7.7
   Compiling actix-rt v2.8.0
   Compiling actix-server v2.2.0
   Compiling h2 v0.3.15
   Compiling actix-codec v0.5.0
   Compiling derive_more v0.99.17
   Compiling actix-macros v0.2.3
   Compiling actix-web-codegen v4.1.0
   Compiling zstd v0.12.3+zstd.1.5.2
   Compiling actix-http v3.3.0
   Compiling actix-web v4.3.0
   Compiling rust-test v0.1.0
    Finished dev [unoptimized + debuginfo] target(s) in 13.25s