「実践Rustプログラミング入門」に入門する(3日目)


pp.190 - 192

p.190 Webフレームワークのセットアップ

PS work> cargo new todo
     Created binary (application) `todo` package
PS work> cd todo
PS work/todo> cargo add actix-web actix-rt
    Updating 'https://github.com/rust-lang/crates.io-index' index
remote: Enumerating objects: 1467, done.
remote: Counting objects: 100% (1467/1467), done.
remote: Compressing objects: 100% (129/129), done.
Rremote: Total 2037 (delta 1409), reused 1393 (delta 1336), pack-reused 570
Receiving objects: 100% (2037/2037), 825.43 KiB | 1.87 MiB/s, done.
Resolving deltas: 100% (1446/1446), completed with 376 local objects.
From https://github.com/rust-lang/crates.io-index
   2092a0fa4..c44091585  master     -> origin/master
      Adding actix-web v2.0.0 to dependencies
      Adding actix-rt v1.1.1 to dependencies
PS work/todo> 

1分かかるかかからないか?Rustは関連ツールも動作が軽快な印象がある。

Cargo.tomlに以下の通り、追記される。

Cargo.toml
[dependencies]
actix-web = "2.0.0"
actix-rt = "1.1.1"

p.192 src/main.rs

src/main.rs
use actix_web::{get, App, HttpResponse, HttpServer};

#[get("/")]
async fn index() -> Result<HttpResponse, actix_web::Error> {
    let response_body = "Hello world!";
    Ok(HttpResponse::Ok().body(response_body))
}

#[actix_rt::main]
async fn main() -> Result<(), actix_web::Error> {
    HttpServer::new(move || App::new().service(index))
        .bind("0.0.0.0:8008")?
        .run()
        .await?;
    Ok(())
}

苦痛ではない記述量。asyncとか、本格的なサービス構築時にありがたい機能がある。
事前調査として、Sinatra(Ruby)でも実験したのが以下のコード。さすがに信じられないくらい短い。

myapp.rb
require 'sinatra'

set :bind, '0.0.0.0'

get '/' do
    'Hello world!'
end

p.192 cargo run

197個のモジュールのコンパイルの後に、サービスが起動する。

PS work/todo> cargo run
   Compiling proc-macro2 v1.0.19
   Compiling unicode-xid v0.2.1
   Compiling syn v1.0.39
   Compiling cfg-if v0.1.10
   Compiling winapi v0.3.9
   Compiling memchr v2.3.3
   Compiling winapi-build v0.1.1
   Compiling libc v0.2.76
   Compiling log v0.4.11
   Compiling lazy_static v1.4.0
   Compiling futures-core v0.3.5
   Compiling slab v0.4.2
   Compiling futures-sink v0.3.5
   Compiling winapi v0.2.8
   Compiling bytes v0.5.6
   Compiling pin-project-internal v0.4.23
   Compiling proc-macro-nested v0.1.6
   Compiling iovec v0.1.4
   Compiling proc-macro-hack v0.5.18
   Compiling once_cell v1.4.1
   Compiling autocfg v1.0.1
   Compiling pin-project-lite v0.1.7                                                   
   Compiling pin-utils v0.1.0
   Compiling futures-io v0.3.5
   Compiling smallvec v1.4.2
   Compiling instant v0.1.6
   Compiling scopeguard v1.1.0
   Compiling bitflags v1.2.1
   Compiling getrandom v0.1.14
   Compiling matches v0.1.8
   Compiling tinyvec v0.3.4
   Compiling adler v0.2.3
   Compiling serde_derive v1.0.115
   Compiling failure_derive v0.1.8
   Compiling copyless v0.1.5                                                           
   Compiling itoa v0.4.6
   Compiling gimli v0.22.0
   Compiling serde v1.0.115
   Compiling percent-encoding v2.1.0
   Compiling either v1.6.0
   Compiling ppv-lite86 v0.2.9
   Compiling rustc-demangle v0.1.16
   Compiling object v0.20.0                                                            
   Compiling unicode-segmentation v1.6.0                                               
   Compiling match_cfg v0.1.0
   Compiling ipconfig v0.2.2                                                           
   Compiling fnv v1.0.7
   Compiling cc v1.0.59
   Compiling linked-hash-map v0.5.3
   Compiling crc32fast v1.2.0
   Compiling widestring v0.4.2
   Compiling byteorder v1.3.4                                                          
   Compiling quick-error v1.2.3                                                        
   Compiling ryu v1.0.5                                                                
   Compiling regex-syntax v0.6.18                                                      
   Compiling serde_json v1.0.57                                                        
   Compiling encoding_rs v0.8.24
   Compiling httparse v1.3.4
   Compiling dtoa v0.4.6
   Compiling mime v0.3.16
   Compiling base64 v0.11.0
   Compiling sha1 v0.6.0
   Compiling language-tags v0.2.2                                                      
   Compiling mio-uds v0.6.8
   Compiling ws2_32-sys v0.2.1
   Compiling kernel32-sys v0.2.2                                                       
   Compiling thread_local v1.0.1                                                       
   Compiling tracing-core v0.1.15
   Compiling futures-channel v0.3.5
   Compiling bytestring v0.1.5                                                         
   Compiling futures-task v0.3.5
   Compiling num-traits v0.2.12
   Compiling hashbrown v0.8.2
   Compiling num-integer v0.1.43
   Compiling indexmap v1.5.2
   Compiling lock_api v0.4.1
   Compiling unicode-bidi v0.3.4
   Compiling miniz_oxide v0.4.1
   Compiling unicode-normalization v0.1.13
   Compiling heck v0.3.1
   Compiling addr2line v0.13.0
   Compiling http v0.2.1
   Compiling lru-cache v0.1.2
   Compiling brotli-sys v0.3.2
   Compiling idna v0.2.0
   Compiling aho-corasick v0.7.13
   Compiling quote v1.0.7
   Compiling tracing v0.1.19
   Compiling num_cpus v1.13.0
   Compiling backtrace v0.3.50
   Compiling rand_core v0.5.1
   Compiling flate2 v1.0.17
   Compiling url v2.1.1                                                                
   Compiling fxhash v0.2.1                                                             
   Compiling net2 v0.2.34
   Compiling parking_lot_core v0.8.0                                                   
   Compiling socket2 v0.3.12                                                           
   Compiling hostname v0.3.1                                                           
   Compiling winreg v0.6.2
   Compiling time v0.1.44
   Compiling regex v1.3.9
   Compiling threadpool v1.8.1                                                         
   Compiling rand_chacha v0.2.2
   Compiling miow v0.2.1
   Compiling parking_lot v0.11.0
   Compiling resolv-conf v0.6.3
   Compiling brotli2 v0.3.2                                                            
   Compiling chrono v0.4.15
   Compiling rand v0.7.3
   Compiling mio v0.6.22                                                               
   Compiling synstructure v0.12.4
   Compiling tokio v0.2.22                                                             
   Compiling futures-macro v0.3.5
   Compiling derive_more v0.99.9
   Compiling actix-macros v0.1.2                                                       
   Compiling async-trait v0.1.40
   Compiling enum-as-inner v0.3.3                                                      
   Compiling actix-web-codegen v0.2.2
   Compiling tokio-util v0.2.0                                                         
   Compiling tokio-util v0.3.1
   Compiling pin-project v0.4.23
   Compiling failure v0.1.8
   Compiling actix-threadpool v0.3.3
   Compiling actix-codec v0.2.0
   Compiling futures-util v0.3.5
   Compiling futures-executor v0.3.5
   Compiling actix-rt v1.1.1
   Compiling actix-service v1.0.6                                                      
   Compiling h2 v0.2.6
   Compiling futures v0.3.5                                                            
   Compiling actix-utils v1.0.6                                                        
   Compiling trust-dns-proto v0.18.0-alpha.2
   Compiling serde_urlencoded v0.6.1                                                   
   Compiling actix-router v0.2.4
   Compiling actix-server v1.0.3
   Compiling actix-tls v1.0.0                                                          
   Compiling trust-dns-resolver v0.18.0-alpha.2                                        
   Compiling actix-testing v1.0.1                                                      
   Compiling actix-connect v1.0.2
   Compiling actix-http v1.0.1                                                         
   Compiling awc v1.0.1
   Compiling actix-web v2.0.0                                                          
   Compiling todo v0.1.0 (work/todo)
    Finished dev [unoptimized + debuginfo] target(s) in 3m 04s                         
     Running `target/debug/todo.exe`

cargo new todoをしてから10分でここまでいける(もちろん使用PCのスペックの差はある)。この軽快さならば、REST APIをリリースした時にも、作業対応速度を十分稼げる。