🐾
WSL(Ubuntu) で Hello, Actix-web!
はじめに
- 本記事では WSL (Ubuntu) における Rust のインストールから、Actix-web の設定までを解説します。
環境
- WSL (Ubuntu)
WSL (Ubuntu) に Rust をインストール
以下のコマンドを実行してインストールします。
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
.profile
ファイルを反映させます。
$ source ~/.profile
バージョンを確認します。
$ rustc --version
$ cargo --version
cargo でプロジェクトを作成
以下のコマンドを実行し、プロジェクトを作成します。
$ cargo new hello_cargo
コンパイルをします。
$ cargo build
以下で実行します。
$ ./target/debug/hello_cargo
コンソールに Hello, world! と表示されます。
Hello, world!
ちなみに、以下のコマンドでコンパイルと実行をまとめてできます。
$ cargo run
バイナリを生成せずに、コンパイルチェックだけを実行する場合
$ cargo check
Actix-web を設定
Cargo.toml
に以下を設定します。
[dependencies]
actix-web = "4"
cargo build
を実行します。
ダウンロードとコンパイルが走ります。少し時間がかかります。
main.rs
に以下のコードを書きます。
use actix_web::{ web, App, HttpResponse, HttpServer };
async fn hello() -> HttpResponse {
HttpResponse::Ok().body("Hello, Actix-web!")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/", web::get().to(hello)))
.bind("127.0.0.1:8080")?
.run().await
}
cargo run
を実行し、http://127.0.0.1:8080/
にアクセスします。
Hello, Actix-web! と表示されれば完了です。お疲れさまでした。
Discussion