I found some code use syntax like where I:
, for example
struct Foo<I> where I: {
i: I
}
This code is written by me for no purpose, but it compiles. Could anyone help what it means and when it is useful to do so?
I found some code use syntax like where I:
, for example
struct Foo<I> where I: {
i: I
}
This code is written by me for no purpose, but it compiles. Could anyone help what it means and when it is useful to do so?
It means it applies zero trait bounds to the type I
. So, it does nothing in this case.
In some complex cases it can be useful to mention a type (that is more than just a bare type variable), without any bounds, to express “this type must be able to exist”; like where [(); N - 1]:
means that the array must be able to exist, i.e. N > 0
.
Also, allowing an empty list of bounds makes it easier to write macros and other code that generates code.
It has an additional, mostly undocumented effect on lifetimes -- it makes the lifetime early-bound.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.