0Pricing
Learn Rust Coding · درس

التوجيه في Axum

المعالجات والمسارات

التوجيه في Axum درس مجاني في Learn Rust Coding على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Learn Rust Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What is Axum?

Axum is an ergonomic, async web framework from the Tokio team. It builds on tower and hyper.

  • Handlers are plain async functions
  • Routing is type-driven and composable
  • Runs on the Tokio runtime

A handler is a function

An Axum handler is an async function that returns something convertible into a response, such as a &str or String.

async fn hello() -> &'static str {
    "Hello, world!"
}

Building a Router

The Router maps paths to handlers. Use route with an HTTP method filter like get.

use axum::{Router, routing::get};

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

fn app() -> Router {
    Router::new().route("/", get(hello))
}

Starting the server

Bind a TCP listener and serve the router. The #[tokio::main] macro sets up the async runtime.

use axum::{Router, routing::get};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hi" }));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

Multiple routes

Chain route calls to register several endpoints on one router.

use axum::{Router, routing::get};

fn app() -> Router {
    Router::new()
        .route("/", get(|| async { "home" }))
        .route("/about", get(|| async { "about" }))
}

Different HTTP methods

Combine method handlers on one path with get(...).post(...). Each method routes to its own handler.

use axum::{Router, routing::get};

fn app() -> Router {
    Router::new().route(
        "/items",
        get(|| async { "list" }).post(|| async { "create" }),
    )
}

Path parameters

Capture parts of the URL with a {name} segment and read them via the Path extractor.

use axum::extract::Path;

async fn show(Path(id): Path<u32>) -> String {
    format!("item {}", id)
}

Modeling routing in plain Rust

At its core, routing maps a path to a response. This runnable model captures that idea without the framework.

fn route(path: &str) -> &str {
    match path {
        "/" => "home",
        "/about" => "about page",
        _ => "404 not found",
    }
}

fn main() {
    println!("{}", route("/"));
    println!("{}", route("/about"));
    println!("{}", route("/missing"));
}

Nesting routers

Compose larger apps by mounting a sub-router under a prefix with nest.

use axum::{Router, routing::get};

fn api() -> Router {
    Router::new().route("/users", get(|| async { "users" }))
}

fn app() -> Router {
    Router::new().nest("/api", api())
}

Fallback handler

Define what happens for unmatched routes using fallback. Perfect for custom 404 pages.

use axum::{Router, http::StatusCode};

async fn not_found() -> (StatusCode, &'static str) {
    (StatusCode::NOT_FOUND, "nothing here")
}

fn app() -> Router {
    Router::new().fallback(not_found)
}

Returning status codes

A handler can return a tuple of (StatusCode, body) to control the response status precisely.

use axum::http::StatusCode;

async fn create() -> (StatusCode, &'static str) {
    (StatusCode::CREATED, "created")
}

Quick Check

Test your understanding of Axum routing.

Recap

You learned Axum routing fundamentals:

  • Handlers are async functions returning a response
  • Router::new().route(path, get(handler)) registers endpoints
  • Path captures URL segments
  • nest, fallback, and status tuples shape the app

Next: extractors parse incoming requests.

الأسئلة الشائعة

هل درس «التوجيه في Axum» مجاني؟

نعم — نص درس «التوجيه في Axum» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Learn Rust Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.

ماذا ستتعلم في «التوجيه في Axum»؟

المعالجات والمسارات تتمرن على Learn Rust Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟

لا تُشترط خبرة سابقة. Learn Rust Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «التوجيه في Axum»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Learn Rust Coding هذا؟

نعم. كل درس في Learn Rust Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. التوجيه في Axum
  2. المستخرِجات
  3. JSON والحالة
  4. البرمجيات الوسيطة
← العودة إلى Learn Rust Coding