Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax and semantics of the `where` keyword?

Tags:

rust

Unfortunately, Rust's documentation regarding where is very lacking. The keyword only appears in one or two unrelated examples in the reference.

  1. 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 } 
  2. 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)

like image 311
urubi Avatar asked Feb 09 '15 08:02

urubi


People also ask

What Is syntax and semantics with examples?

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.

What are syntax and semantics?

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.

How do you use syntax and semantics?

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.

What is the syntax of words?

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.


1 Answers

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

like image 53
Levans Avatar answered Sep 19 '22 18:09

Levans