【转载】2023 年最好的 Rust Web 框架

2023 年最好的 Rust Web 框架

在 2023 年,哪个 rust Web 框架最适合开发 API 和 Web 应用程序?在这里,我们将看看目前使用的一些最流行的框架。我们还将探讨 rust Web 开发功能和代码示例。

Web 框架是一个软件库,它提供了一种构建和部署 Web 应用程序的标准方法。Web 框架通常包括用于日常任务的库,例如路由(将 URL 映射到资源)、模板化(从数据生成 HTML)和数据库访问。

Rust 是一种编程语言,为 Web 开发提供了巨大的潜力。它快速、安全,并为构建 Web 应用程序提供了许多完美的功能。以下是一些最好的框架及其功能。

Rocket

Rocket 是 Rust 的一个 Web 框架,它使编写快速的 Web 应用程序变得简单,而不会牺牲灵活性或类型安全性。Rocket 遵循"约定重于配置"的理念,旨在让您以最少的配置启动和运行。它与 Rust 生态系统紧密集成,使使用现有的库和工具变得容易。

下面是一个基本示例,index被挂载到/api上:

#[macro_use] extern crate rocket;


#[get("/")]
fn index() -> &'static str {
    
    
    "Hello, world!"
}

#[launch]
fn rocket() -> _ {
    
    
    rocket::build()
    .mount(
        "/api", 
        routes![
            index
        ]
    )
}

使用cargo run运行代码示例。

Tide

Tide 是 Rust 编程语言的 Web 框架。它提供了一个干净且易于使用的 API 来构建 Web 应用程序。Tide 基于 rust actix Web 框架。

Tide特点:

  • 异步/等待支持
  • 类型安全路由
  • 请求守卫
  • 模板支持
  • Session管理
  • WebSocket

代码示例:对于使用 Tide 的"Hello,world" Web应用程序:

use tide::{
    
    Request, Response};

async fn hello(_: Request) -> Response {
    
    
    Response::new(200).body_string("Hello, world!")
}

fn main() {
    
    
    let mut app = tide::new();
    app.at("/").get(hello);
    app.run("127.0.0.1:8080").unwrap();

}

这将启动一个 Web 服务器侦听 http://127.0.0.1:8080,该服务器以"Hello, world!"返回

Actix-web

Actix-web基于Actor模型。它是一个用 Rust 编写的高速 Web 框架。Actix-web 为构建 Web 应用程序提供了一组强大的功能,包括:

  • 支持多路复用
  • 异步 I/O
  • WebSocket
  • 中间件支持

代码示例:

use actix_web::{
    
    web, App, Responder};

async fn index() -> impl Responder {
    
    
    "Hello, world!"
}

fn main() {
    
    
    App::new().route("/", web::get().to(index)).run();
}

使用cargo run运行代码示例。

Warp

Warp是一个建立在东京之上的快速,灵活的Web服务器框架。它易于使用,并具有许多开箱即用的功能,例如:

  • 自动TLS
  • 支持 HTTP/2
  • 服务器发送的事件(SSE)
  • WebSocket
  • 路径参数
  • 查询参数
  • JSON 正文解析
  • 多部分表单
  • 静态文件服务

下面是一个使用 Warp 的简单“Hello, world”示例:

use warp::Filter;

fn main() {
    
    

    let hello = warp::path!("hello") // Match /hello/...
    .and(warp::path::end()) // ...but only at the end (no more '/'s after this)
    .map(|| "Hello, world!"); // Respond with a simple body

    warp::serve(hello).run((["127.0.0.1", "8080"]).into());
}

Axum

该 Web 框架旨在快速、高效和轻量级。Erlang 编程语言启发了它,为开发人员提供了高并发级别。Axum 非常适合构建微服务、实时应用程序和低延迟系统。

其功能包括:

  • 受 Erlang 启发的并发执行组件模型
  • 低延迟和高性能
  • 支持 WebSocket 和其他协议
  • 异步 I/O

代码示例:

use actix_web::{
    
    web, App, Responder};

fn index() -> impl Responder {
    
    

    "Hello, world!"

}

fn main() {
    
    

    App::new().route("/", web::get().to(index)).run();

}

Hyper

该框架建立在 Rust 标准库之上,并为广泛的功能提供支持。其中包括:

  • 异步 I/O:这允许您使用 async/await 等功能来提高代码效率
  • HTTP2支持:这意味着您可以利用Web开发中的最新标准
  • 快速轻量级:Hyper 设计为快速轻量级,因此非常适合开发高性能 Web 应用程序
  • 易于使用:该框架易于使用,因此您可以快速开始开发 Web 应用程序
  • 它也毫不费力地使用,并且具有出色的文档。

代码示例:

extern crate hyper;

use hyper::{
    
    Body, Response, Server};
use hyper::rt::Future;
use hyper::service::service_fn;

const PHRASE: &str = "Hello, World!";

fn main() {
    
    
    // Bind to the address and call the `run` function, passing in a closure.
    // The closure contains our entire HTTP service.
    let addr = "127.0.0.1:3000".parse().unwrap();
    let server = Server::bind(&addr)
    .serve(|| service_fn(|_| {
    
    
        // We return a `Future` that resolves to a `Result` when the HTTP
        // body has been fully read. In this case, just return an empty
        // successful result.
        future::ok(Response::new(Body::from(PHRASE)))

    }))
    .map_err(|e| {
    
    
        // If any error happens, log it to STDOUT.
        println!("server error: {}", e);
    });

    // Run this HTTP service on the current thread.
    hyper::rt::run(server);
}

任何使用 rust API 框架的开发人员都需要了解 Hyper。它具有许多功能,并且易于使用。它的文档也是一流的,使其成为新开发人员的绝佳学习资源。


总结

这些只是一些最好的 rust Web 编程框架。它们都有优点和缺点,因此由您决定哪种最适合您的需求。这些 Rust Web 框架比其他框架具有优势,包括:

  • 优异的性能
  • 卓越的内存安全性
  • 广泛的功能
  • 易于使用

如果您正在寻找具备了这些优点的 rust Web 框架,那么您绝对应该查看这些框架之一,它们是开发 Web 应用程序的绝佳选择。

猜你喜欢

转载自blog.csdn.net/m0_51810668/article/details/130716872