Unfortunately, Rust's documentation regarding where
is very lacking. The keyword only appears in one or two unrelated examples in the reference.
What semantic difference does where
make in the following code? Is there any difference at all? Which form is preferred?
fn double_a<T>(a:T) -> T where T:std::num::Int { a+a } fn double_b<T: std::num::Int>(a:T) -> T { a+a }
In the implementation of the CharEq trait, it seems that where
is being used as some sort of "selector" to implement Trait for anything that matches some closure type. Am I correct?
Is there any way I can get a better, more complete picture of where
? (full specification of usage and syntax)
Syntax is the grammatical structure of the text, whereas semantics is the meaning being conveyed. A sentence that is syntactically correct, however, is not always semantically correct. For example, “cows flow supremely” is grammatically valid (subject — verb — adverb) but it doesn't make any sense.
Put simply, syntax refers to grammar, while semantics refers to meaning. Syntax is the set of rules needed to ensure a sentence is grammatically correct; semantics is how one's lexicon, grammatical structure, tone, and other elements of a sentence coalesce to communicate its meaning.
Syntax and Semantics. Syntax is the arrangement of elements and attributes to create well-formed documents. Semantics is concerned with meaning. In HTML, this is the purpose of elements and attributes, and the logical (sense and reference) relationship between elements and the attributes of those elements.
Syntax is basically about what word comes before and after another word; in other words, it's part of the larger subject of grammar. Syntax is often an issue in poetry, and it's usually discussed in connection with diction—that is, the poet's choice of words.
In your example, the two codes are strictly equivalent.
The where
clauses were introduced to allow more expressive bound-checking, doing for example :
fn foo<T>(a: T) where Bar<T>: MyTrait { /* ... */ }
Which is not possible using only the old syntax.
Using where
rather than the original syntax is generally preferred for readability even if the old syntax can still be used.
You can imagine for example constructions like
fn foo<A, B, C>(a: A, b: B, c: C) where A: SomeTrait + OtherTrait, B: ThirdTrait<A>+ OtherTrait, C: LastTrait<A, B> { /* stuff here */ }
which are much more readable this way, even if the could still be expressed using the old syntax.
For your question about the CharEq
trait, the code is:
impl<F> CharEq for F where F: FnMut(char) -> bool { #[inline] fn matches(&mut self, c: char) -> bool { (*self)(c) } #[inline] fn only_ascii(&self) -> bool { false } }
It literally means: Implementation of trait CharEq
for all type F
that already implements the trait FnMut(char) -> bool
(that is, a closure or a function taking a char
and returning a bool
).
For more details, you can look at the RFC that introduced the where
clauses : https://github.com/rust-lang/rfcs/pull/135
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With