yageek
yageek

Reputation: 4464

Rust trait bounds with types

I was reading the hyperium/http source code and I found this:

pub fn put<T>(uri: T) -> Builder
 where
    Uri: TryFrom<T>,
    <Uri as TryFrom<T>>::Error: Into<crate::Error>,

{
    Builder::new().method(Method::PUT).uri(uri)
}   

In this snippet, Uri is a type and T a generic element. I have always seen the construction where T: SomeTrait, but not SomeType: SomeTrait<T>. Does this construction have a name and is it documented somewhere?

Upvotes: 0

Views: 2865

Answers (1)

phimuemue
phimuemue

Reputation: 36071

Quoting https://doc.rust-lang.org/reference/trait-bounds.html#higher-ranked-trait-bounds:

Bounds on an item must be satisfied when using the item. When type checking and borrow checking a generic item, the bounds can be used to determine that a trait is implemented for a type. For example, given Ty: Trait

In the body of a generic function, methods from Trait can be called on Ty values. Likewise associated constants on the Trait can be used. Associated types from Trait can be used. Generic functions and types with a T: Trait bounds can be used with Ty being used for T.

Nothing states that Ty is a type parameter (and not a fixed type). So I would say it is simply a trait bound, albeit admittedly not very often encountered in Rust tutorials.

Upvotes: 1

Related Questions