Get started with "Introduction to Practical Rust Programming" (Day 3)

pp.190 - 192

p.190 Web framework setup

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> 

Doesn't it take a minute? Rust has the impression that related tools work lightly.

It will be added to Cargo.toml as follows.

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(())
}

The amount of description that is not painful. There are functions such as ʻasync` that are appreciated when building a full-scale service. As a preliminary survey, the following code was also experimented with Sinatra (Ruby). It's incredibly short.

myapp.rb


require 'sinatra'

set :bind, '0.0.0.0'

get '/' do
	'Hello world!'
end

p.192 cargo run

After compiling 197 modules, the service starts.

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`

20200902.png

You can reach this point in 10 minutes after doing cargo new to do (of course, there are differences in the specifications of the PC used). With this lightness, even when the REST API is released, you can earn enough work response speed.

Recommended Posts

Get started with "Introduction to Practical Rust Programming" (Day 3)
Let's get started with parallel programming
How to get started with slim
I tried to get started with WebAssembly
[Note] How to get started with Rspec
How to get started with Eclipse Micro Profile
Rails beginners tried to get started with RSpec
Get started with Gradle
I tried to get started with Spring Data JPA
How to get started with creating a Rails app
Get started with Spring boot
Get started with DynamoDB with docker
How to get started with Gatsby (TypeScript) x Netlify x Docker
Now is the time to get started with the Stream API
How to get started with JDBC using PostgresSQL on MacOS
I tried to get started with Swagger using Spring Boot
Easy code review to get started with Jenkins / SonarQube: Static analysis
Introduction to Functional Programming (Java, Javascript)
Introduction to algorithms with java-Shakutori method
Introduction to Programming for College Students: Introduction
How to get along with Rails
Memo to get with Struts2 + Ajax
Start with a browser Ruby game programming: Introduction to Nyle-canvas (DXRuby style)
Introduction to Programming for College Students: Variables
Introduction to Ruby basic grammar with yakiniku
Introduction to Docker / Kubernetes Practical Container Development
Introduction to algorithms with java --Search (breadth-first search)
Introduction to Robot Battle with Robocode (Environment Construction)
Let's get started with Java-Create a development environment ②
Let's get started with Java-Create a development environment ①
Introduction to algorithms with java --Search (bit full search)
Introduction to algorithms with java-Search (Full search, Binary search)
How to get today's day of the week
Introduction to kotlin for iOS developers ⑤-Practical XML
Getting started with Kotlin to send to Java developers
Maybe it works! Let's get started with Docker!
Try to get redmine API key with ruby
How to get resource files out with spring-boot
Getting Started with Doma-Introduction to the Criteria API
Introduction to Robot Battle with Robocode (Beginner Development)
[Introduction to Spring Boot] Authentication function with Spring Security
[Introduction to Java] List of things that got caught by the 14th day of programming
[DDD] What is the most accessible architecture to get started with Domain Driven Design?