From 5a0f18558efc7ddb9cee6788bf74aa9876682bfa Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Thu, 27 Oct 2011 08:36:06 -0700 Subject: [PATCH] Add 'as' (cast) operator to the documentation --- doc/rust.texi | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/doc/rust.texi b/doc/rust.texi index fcca023d2553..eacd692d1600 100644 --- a/doc/rust.texi +++ b/doc/rust.texi @@ -2930,6 +2930,7 @@ effects of the expression's evaluation. * Ref.Expr.Ret:: Expression for stopping and producing a value. @c * Ref.Expr.Be:: Expression for stopping and executing a tail call. * Ref.Expr.Put:: Expression for pausing and producing a value. +* Ref.Expr.As:: Expression for casting a value to a different type. * Ref.Expr.Fail:: Expression for causing task failure. * Ref.Expr.Log:: Expression for logging values to diagnostic buffers. * Ref.Expr.Note:: Expression for logging values during failure. @@ -3116,6 +3117,30 @@ either continuing execution after the @code{put} expression, or terminating its execution and destroying the iterator frame. +@node Ref.Expr.As +@subsection Ref.Expr.As +@c * Ref.Expr.As:: Expression for casting a value to a different type. +@cindex As expression +@cindex Cast +@cindex Typecast + +Executing an @code{as} expression casts the value on the left-hand side to the +type on the right-hand side. + +A numeric value can be cast to any numeric type. A native pointer value can +be cast to or from any integral type or native pointer type. Any other cast +is unsupported and will fail to compile. + +An example of an @code{as} expression: +@example +fn avg(v: [float]) -> float @{ + let sum: float = sum(v); + let sz: float = std::vec::len(v) as float; + ret sum / sz; +@} +@end example + + @node Ref.Expr.Fail @subsection Ref.Expr.Fail @c * Ref.Expr.Fail:: Expression for causing task failure.