Let’s say I have this code:
async fn get_product_data(product_id: Uuid) -> () {
let product_with_skus: Vec<(product::Model, Vec<sku::Model>)> = Product::find_by_id(product_id)
.find_with_related(Sku)
.all(&*DB)
.await
.unwrap();
match product_with_skus {
Vec<(product::Model, Vec<sku::Model>)> => "prd".to_string(),
_ => "trt".to_string(),
};
}
product_with_skus
is of type Vec<(product::Model, Vec<sku::Model>)>
, also inferred correctly by the compiler itself. However, when I try to match it, it says Generic args in patterns require turbofish syntax.
I already tried:
Vec::<(product::Model, Vec::<sku::Model>)>
, no luck.
How and where can I place turbofish? Please do not suggest changing that to two queries and using helper structs.
7
The problem with Vec::<(product::Model, Vec::<sku::Model>)>
is that while it correctly employs a turbofish, it’s not a complete pattern, for non-unit structs like Vec
you need a struct body {..}
:
async fn get_product_data(product_id: Uuid) -> () {
let product_with_skus = Product::find_by_id(product_id)
.find_with_related(Sku)
.all(&*DB)
.await
.unwrap();
match product_with_skus {
Vec::<(product::Model, Vec::<sku::Model>)>{ .. } => "prd".to_string(),
};
}
Notes:
- This is an irrefutable pattern so any
_
arm is superfluous and cannot be reached. - Since
Vec
s fields are private you can’t match them outside ofalloc::vec
- While you can specify the type this way, it still has to be the same across all match arms.
- Specifying the type on both
let product_with_skus: Vec<(product::Model, Vec<sku::Model>)>
and in the match is redundant, pick one and stick to it (personally I’d stick to specifing it in thelet
binding since I’ve never seen a type hint in amatch
).