Understanding where clause

Viewed 76

I'm trying to understand the where clause, the rust book shows me the normal where clause:

where
    A: Trait + TraitB,

But as I start reading code, I start seeing more of

where
    A: TraitA,
    A::TraitType: TraitB,

Correct me if I'm wrong but here where we're dealing with associate types. Where the type TraitType in TraitA must implement TraitB. Now what does the below mean especially the command followed by a struct?

where
    A: TraitA, { StructA { f } }
1 Answers

Correct if i wrong but thats where we dealing with associate types. where the type TraitType in TraitA must implement TraitB.

That is correct.

now what does the below mean especially the command followed by a struct:

where
   A: TraitA, { StructA { f } }

That's just a function body that returns an instance of StructA. It means the same thing as:

where A: TraitA
{
    return StructA {
        f: f
    };
}

Except the code in your question uses a trailing expression to return the value instead of an explicit return statement. Trailing expressions are the preferred way to return values in Rust; explicit return is usually only used to return early, e.g. out of an if branch or a match arm.

Related