-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Document where clauses. #22123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document where clauses. #22123
Conversation
r? @huonw (rust_highfive has picked a reviewer for you, use r? to override) |
It might be worth mentioning that fn foo<T>(x: T) where i32: Trait<T> {
1i32.method(x)
} which is the main motivation for them, rather than just syntactic nicety. |
use std::fmt::Debug; | ||
|
||
fn bar<T, K>(x: T, y: K) | ||
where T: Clone, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a set style for this? The most common I've seen is:
fn bar<T, K>(x: T, y: K)
where T: Clone, K: Clone + Debug {
..
}
and/or
fn bar<T, K>(x: T, y: K)
where T: Clone,
K: Clone + Debug {
..
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was also asked at http://internals.rust-lang.org/t/multiple-line-fn-and-impl-declarations/1106 but no one responded. His preference was 2 tab indentation for where
.
fn bar<T, K>(x: T, y: K)
where T: Clone,
K: Clone + Debug {
..
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we don't have one so I did what I'd do, and figured someone would mention it 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've seen this pattern floating around a little also:
fn bar<T, K>(x: T, y: K)
where
T: Clone,
K: Clone + Debug,
{
..
}
I find it nicely readable for situations where there are multiple types and many bounds, easy to refactor and plays nice with auto-indent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also recall seeing this somewhere in the std src:
fn bar<T, K>(x: T, y: K) where
T: Clone,
K: Clone + Debug {
..
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mitchmindtree The last one looks great visually, especially if one squints :). But is just begs for {
to be on the next line for such multiline case. Also, what about the result:
fn bar<T, K>(x: T, y: K)
-> Result<bool, String>
where
T: Clone,
K: Clone + Debug
{
..
}
@huonw can you give a bigger example of that? I've never seen that before. |
trait ConvertTo<Output> {
fn convert(&self) -> Output;
}
impl ConvertTo<i64> for i32 {
fn convert(&self) -> i64 { *self as i32 }
}
// can be called with T == i32
fn normal<T: ConvertTo<i64>>(x: &T) -> i64 {
x.convert()
}
// can be called with T == i64
fn inverse<T>() -> T
// this is using ConvertTo as if it were "ConvertFrom<i32>"
where i32: ConvertTo<T> {
1i32.convert()
} |
another use-case and pretty explanation on stackoverflow: http://stackoverflow.com/questions/28405621/what-is-the-syntax-and-semantics-of-the-where-keyword |
7a314fa
to
068b0cd
Compare
Added. The only thing left is the style question. |
|
||
This flexibility can add clarity in complex situations. | ||
|
||
`where` is also more powerful than the simpler syntax. For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this could be expanded to be specific about what is happening: where clauses allow bounds where the left-hand side is an arbitrary type, not just a plain type parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
068b0cd
to
faf0f5b
Compare
@huonw r? |
@bors r+ faf0 rollup |
Closes #21859.