Troubleshooting - Help and questions

Axum panics with "Path segments must not start with `:`". How do I fix it?

#axum#routing#troubleshooting#upgradingasked 0 answers0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Axum 0.8 changed the path syntax from /:id and /*rest to /{id} and /{*rest}. A route written the old way panics when the Router is built. Change the colon form to braces, or call without_v07_checks on the router if you really need a literal colon.

Checked with rustc 1.93.0, axum 0.8.9, Windows 11; the panic below is the real output of the program shown

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Symptom#

The program panics at start-up, while the router is being built:

text
thread 'main' (43060) panicked at examples\f04.rs:7:38:
Path segments must not start with `:`. For capture groups, use `{capture}`. If you meant to literally match a segment starting with a colon, call `without_v07_checks` on the router.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

It comes from code like this:

src/main.rsrust
use axum::{extract::Path, routing::get, Router};

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

#[tokio::main]
async fn main() {
    let _app: Router = Router::new().route("/users/:id", get(show));
    println!("router built");
}

Cause#

Axum 0.8 uses {name} for a parameter and {*name} for a wildcard. The older :name and *name forms from axum 0.7 and earlier are rejected when the route is added, so the mistake shows at start-up and not on the first request.

Fix#

rust
Router::new().route("/users/{id}", get(show));      // was "/users/:id"
Router::new().route("/files/{*path}", get(rest));   // was "/files/*path"

If a path really contains a literal segment that starts with a colon, the panic message names the way out: call without_v07_checks on the router.

Common mistakes#

  • Fixing one route and stopping. The panic names the file and line of the first bad route only, so run again after each fix until the router builds.
  • Copying a tutorial written for axum 0.6 or 0.7. Check the axum version in the tutorial against your Cargo.toml.

Is the change only for paths?#

This thread covers only paths. Other 0.8 changes are listed in the axum changelog.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No answers yet

    No answers yet. If you know, answer it.