當前位置:首頁 > IT技術(shù) > 其他 > 正文

【Axum】返回普通文本
2022-04-29 14:07:16

環(huán)境

  • Time 2022-01-16
  • Rust 1.58.0
  • Axum 0.4.4

概念

參考:https://github.com/tokio-rs/axum/blob/main/examples/hello-world/src/main.rs

示例

main.rs

use axum::{routing::get, Router};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(home));

    let addr = SocketAddr::from(([127, 0, 0, 1], 4444));
    println!("listening on {addr}");
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn home() -> &'static str {
    "hello world"
}

總結(jié)

使用 axum 啟動了一個服務(wù)器,訪問時返回普通文本 hello world。

附錄

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >