Fazal Rasel
Fazal Rasel

Reputation: 4526

What is the name of Rust function signatures with type on left of parameter?

From Axum Example: https://github.com/tokio-rs/axum/examples/query-params-with-empty-strings/src/main.rs#L23-L25

async fn handler(Query(params): Query<Params>) -> String {
    format!("{:?}", params)
}

What is the name of the signature pattern when Query(params) is used on param left?

Upvotes: 1

Views: 269

Answers (1)

Masklinn
Masklinn

Reputation: 42492

What is the name of the signature pattern when Query(params) is used on param left?

It's literally just a pattern (as in pattern-matching): in Rust, a binding (a parameter, or the left-hand side of a let, or the left hand side of a for) can be a pattern as long as it's infallible (/ irrefutable*). match and friends are necessary for refutable patterns (patterns which can fail, because they only match a subset of all possible values).

Some languages call this destructuring but Rust doesn't make a difference.

For Axum, it's just a convenient way of accessing the contents of the query parameter (or other extractors), you could also write this as:

async fn handler(query: Query<Params>) -> String {
    format!("{:?}", query.0)
}

or

async fn handler(query: Query<Params>) -> String {
    let Query(params) = query;
    format!("{:?}", params)
}

Upvotes: 5

Related Questions