LoginSignup
0
1

More than 1 year has passed since last update.

Rustにそっと触れてみました

Posted at

環境構築

// インストール
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
// パスを通す
source ~/.cargo/env
// プロジェクト作成
cargo new rust-practice
// ビルド + 実行
cargo run

GETしてみる

Cargo.tomlの[dependencies]にかく

[dependencies]
tokio = { version = "0.2", features = ["full"] }
warp = "0.2"

main.rsに下記をかいて

use warp::Filter;

#[tokio::main]
async fn main() {
    // GET /hello/warp => 200 OK with body "Hello, warp!"
    let hello = warp::path!("hello" / String)
        .map(|name| format!("Hello, {}!", name));

    warp::serve(hello)
        .run(([127, 0, 0, 1], 3030))
        .await;
}

APIテスターとかで

GETでhttp://localhost:3030/hello するとHello Worldできる

main.rsに

use warp::Filter;

#[tokio::main]
async fn main() {
    // GET /hello/warp => 200 OK with body "Hello, warp!"
    let hello = warp::path!("hello" / String)
        .map(|name| format!("Hello, {}!", name));



        // GET /sum/:u32/:u32
        let sum = warp::path("sum")
        .and(warp::path::param::<u32>())
        .and(warp::path::param::<u32>())
        .map(|a, b| format!("{} + {} = {}", a, b, a + b));
        // serverの起動
        warp::serve(sum)
            .run(([127, 0, 0, 1], 3030))
            .await;
    }

ターミナルで

cargo run

ブラウザ でURL http://localhost:3030/sum/1/2 をたたくと

1 + 2 = 3

と表示される

参考サイト

https://pione30.hatenablog.com/entry/2020/11/22/204329

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1