Potato
Home
入门示例
Home
入门示例
  • 前言
  • 入门示例
  • 处理函数标注
  • 处理函数声明
  • 服务端路由
  • 优雅退出
  • 使用客户端

入门示例

从最简单的示例开始,创建Rust项目,并加入potato依赖:

cargo new hello_potato
cd hello_potato
cargo add potato

服务器端

示例代码:

use potato::*;

#[http_get("/hello")]
async fn hello() -> HttpResponse {
    HttpResponse::html("hello world")
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut server = HttpServer::new("0.0.0.0:8080");
    println!("visit: http://127.0.0.1:8080/hello");
    server.serve_http().await
}

客户端

示例代码:

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let res = potato::get("https://www.fawdlstty.com", vec![]).await?;
    println!("{}", String::from_utf8(res.body)?);
    Ok(())
}
最近更新:: 2025/4/17 00:08
Contributors: fawdlstty
Prev
前言
Next
处理函数标注