Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rust, is "as" an operator?

The Rust Reference presently says the following about the as operator:

7.2.12.5 Type cast expressions

A type cast expression is denoted with the binary operator as.

Executing an as expression casts the value on the left-hand side to the type on the right-hand side.

An example of an as expression:

fn average(values: &[f64]) -> f64 {
  let sum: f64 = sum(values);
  let size: f64 = len(values) as f64;
  sum / size
}

(Also, since it will be relevant:

7.2.12.8 Operator precedence

The precedence of Rust binary operators is ordered as follows, going from strong to weak:

as
* / %
+ -
<< >>

)

Naïvely using this as an operator doesn't seem to work:

fn main() {
    let x = 100 as u16 << 8;
}

Doesn't actually compile:

% rustc testing.rs
testing.rs:2:24: 2:25 error: expected type, found `8`
testing.rs:2    let x = 100 as u16 << 8;

With parentheses — let x = (100 as u16) << 8; — it compiles. The parens aren't required in the example in the reference, but seem to be here. What's the exact syntax here? Are parentheses required unless this is the only thing right of an =? Or am I just doing something wrong?

It is a bit weird that this is called an operator, as the RHS would seem to need to be a type, and typically, I think of an operator as taking two expressions…

like image 694
Thanatos Avatar asked Aug 19 '15 18:08

Thanatos


People also ask

What is the operator in Rust?

operator in Rust is used as an error propagation alternative to functions that return Result or Option types. The ? operator is a shortcut as it reduces the amount of code needed to immediately return Err or None from the types Result<T, Err> or Option in a function.

What is == in Rust?

Less than or equal to. Returns true if the left operand is less than or equal to the right operand. == Equal to. Return true if the left operand is equal right operand.

What is the symbol for Rust?

The chemical formula for rust is Fe2O3 and is commonly known as ferric oxide or iron oxide.

WHAT IS A in Rust?

The 'a reads 'the lifetime a'. Technically, every reference has some lifetime associated with it, but the compiler lets you elide (i.e. omit, see "Lifetime Elision") them in common cases. fn bar<'a>(...) A function can have 'generic parameters' between the <> s, of which lifetimes are one kind.

What does the question mark mean in Rust?

The question mark operatorunwraps valid values or returns errornous values, propagating them to the calling function. It is a unary postfix operator that can only be applied to the types Result<T, E> and Option<T> .


1 Answers

The trick here is as takes a type on its right hand side, i.e. the grammar of as looks something like: is expression 'as' type. The expression after the as looks a bit like (the start of) a type, it's trying to parse u16<<... as if u16 had a type parameter (an example of a type with a prefix like that would be Foo<<T>::Bar>).

This is basically just behaviour particular to << because it looks like the type parameter delimiters. If one uses an operator that can't appear after the leading identifier in a type, it works fine:

fn main() {
    let x = 100 as u16 - 8;
}
like image 95
huon Avatar answered Sep 28 '22 06:09

huon