@@ -1701,12 +1701,14 @@ import std::option::*;
1701
1701
import std::str::@{ char_at, hash@} ;
1702
1702
1703
1703
fn main() @{
1704
- // Equivalent to 'log std::math::sin(1.0);'
1705
- log sin(1.0);
1706
- // Equivalent to 'log std::option::some(1.0);'
1707
- log some(1.0);
1708
- // Equivalent to 'log std::str::hash(std::str::char_at("foo"));'
1709
- log hash(char_at("foo"));
1704
+ // Equivalent to 'log(info, std::math::sin(1.0));'
1705
+ log(info, sin(1.0));
1706
+
1707
+ // Equivalent to 'log(info, std::option::some(1.0));'
1708
+ log(info, some(1.0));
1709
+
1710
+ // Equivalent to 'log(info, std::str::hash(std::str::char_at("foo")));'
1711
+ log(info, hash(char_at("foo")));
1710
1712
@}
1711
1713
@end example
1712
1714
@@ -1801,7 +1803,7 @@ A special kind of function can be declared with a @code{!} character where the
1801
1803
output slot type would normally be. For example:
1802
1804
@example
1803
1805
fn my_err(s: str) -> ! @{
1804
- log s ;
1806
+ log(info, s) ;
1805
1807
fail;
1806
1808
@}
1807
1809
@end example
@@ -2767,6 +2769,9 @@ effects of the expression's evaluation.
2767
2769
2768
2770
@menu
2769
2771
* Ref.Expr.Copy :: Expression for copying a value.
2772
+ * Ref.Expr.Move :: Expression for moving a value.
2773
+ * Ref.Expr.Swap :: Expression for swapping two values.
2774
+ * Ref.Expr.Assign :: Expression for moving a copy of a value.
2770
2775
* Ref.Expr.Call :: Expression for calling a function.
2771
2776
* Ref.Expr.Bind :: Expression for binding arguments to functions.
2772
2777
* Ref.Expr.Ret :: Expression for stopping and producing a value.
@@ -2788,43 +2793,125 @@ effects of the expression's evaluation.
2788
2793
* Ref.Expr.AnonObj :: Expression for extending objects with additional methods.
2789
2794
@end menu
2790
2795
2791
-
2792
2796
@node Ref.Expr.Copy
2793
2797
@subsection Ref.Expr.Copy
2794
2798
@c * Ref.Expr.Copy:: Expression for copying a value.
2795
2799
@cindex Copy expression
2796
- @cindex Assignment operator , see @i {Copy expression }
2800
+ @cindex Copy operator , see @i {Move expression }
2797
2801
2798
- A @dfn {copy expression } consists of an @emph { lval } followed by an equals-sign
2799
- ( @code { = }) and a primitive expression. @xref { Ref.Expr } .
2802
+ A unary @dfn {copy expression } consists of the unary @code { copy } operator
2803
+ applied to some argument expression.
2800
2804
2801
- Executing a copy expression causes the value denoted by the expression --
2802
- either a value or a primitive combination of values -- to be copied into the
2803
- memory location denoted by the @emph { lval } .
2805
+ Evaluating a copy expression first evaluates the argument expression, then
2806
+ performs a copy of the resulting value, allocating any memory necessary to
2807
+ hold the new copy .
2804
2808
2805
- A copy may entail the adjustment of reference counts, execution of destructors,
2806
- or similar adjustments in order to respect the path through the memory graph
2807
- implied by the @code {lval }, as well as any existing value held in the memory
2808
- being written-to. All such adjustment is automatic and implied by the @code {= }
2809
- operator.
2809
+ Shared boxes (type @code {@@ }) are, as usual, shallow-copied, as they may be
2810
+ cyclic. Unique boxes, vectors and similar unique types are deep-copied.
2810
2811
2811
- An example of three different copy expressions:
2812
+ Since the assignment operator @code {= } performs a copy implicitly, the unary
2813
+ copy operator is typically only used to cause an argument to a function should
2814
+ be copied, and the copy passed by-value. @xref {Ref.Expr.Assign }.
2815
+
2816
+ An example of a copy expression:
2812
2817
@example
2813
- x = y;
2814
- x.y = z;
2815
- x.y = z + 2;
2818
+ fn mutate(vec: [mutable int]) @{
2819
+ vec[0] = 10;
2820
+ @}
2821
+
2822
+ let v = [mutable 1,2,3];
2823
+
2824
+ mutate(copy v); // Pass a copy
2825
+
2826
+ assert v[0] == 1; // Original was not modified
2816
2827
@end example
2817
2828
2829
+
2830
+ @node Ref.Expr.Move
2831
+ @subsection Ref.Expr.Move
2832
+ @c * Ref.Expr.Move:: Expression for moving a value.
2833
+ @cindex Move expression
2834
+ @cindex Move operator , see @i {Move expression }
2835
+
2836
+ A @dfn {move experssion } consists of an @emph {lval } followed by a left-pointing
2837
+ arrow (@code {<- }) and an @emph {rval } expression. @xref {Ref.Expr }.
2838
+
2839
+ Evaluating a move expression causes, as a side effect, the @emph {rval } to be
2840
+ @emph {moved } into the @emph {lval }. If the @emph {rval } was itself an @emph {lval },
2841
+ it must be a local variable, as it will be de-initialized in the process.
2842
+
2843
+ Evaluating a move expression does not effect reference counts nor does it
2844
+ cause a deep copy of any unique structure pointed-to by the moved
2845
+ @emph {rval }. Instead, the move expression represents an indivisible
2846
+ @emph {transfer of ownership } from the right-hand-side to the left-hand-side of
2847
+ the expression. No allocation or destruction is entailed.
2848
+
2849
+ An example of three different move expressions:
2850
+ @example
2851
+ x <- a;
2852
+ x[i] <- b;
2853
+ x.y <- c;
2854
+ @end example
2855
+
2856
+
2857
+ @node Ref.Expr.Swap
2858
+ @subsection Ref.Expr.Swap
2859
+ @c * Ref.Expr.Swap:: Expression for swapping two values.
2860
+ @cindex Swap expression
2861
+ @cindex Swap operator , see @i {Move expression }
2862
+
2863
+ A @dfn {swap experssion } consists of an @emph {lval } followed by a bi-directional
2864
+ arrow (@code {<-> }) and another @emph {lval } expression. @xref {Ref.Expr }.
2865
+
2866
+ Evaluating a swap expression causes, as a side effect, the vales held in the
2867
+ left-hand-side and right-hand-side @emph {lvals } to be exchanged indivisibly.
2868
+
2869
+ Evaluating a move expression does not effect reference counts nor does it
2870
+ cause a deep copy of any unique structure pointed-to by the moved
2871
+ @emph {rval }. Instead, the move expression represents an indivisible
2872
+ @emph {exchange of ownership } between the right-hand-side to the left-hand-side
2873
+ of the expression. No allocation or destruction is entailed.
2874
+
2875
+ An example of three different swap expressions:
2876
+ @example
2877
+ x <-> a;
2878
+ x[i] <-> b[i];
2879
+ x.y <-> a.b;
2880
+ @end example
2881
+
2882
+
2883
+ @node Ref.Expr.Assign
2884
+ @subsection Ref.Expr.Assign
2885
+ @c * Ref.Expr.Copy:: Expression for moving a copy of a value.
2886
+ @cindex Assignment expression
2887
+ @cindex Assignment operator , see @i {Assignment expression }
2888
+
2889
+ An @dfn {assignment expression } consists of an @emph {lval } expression followed
2890
+ by an equals-sign (@code {= }) and an @emph {rval } expression. @xref {Ref.Expr }.
2891
+
2892
+ Evaluating an assignment expression is equivalent to evaluating a move
2893
+ expression combined with a unary copy expression. For example, the following
2894
+ two expressions have the same effect:
2895
+
2896
+ @example
2897
+ x = y
2898
+ x <- copy y
2899
+ @end example
2900
+
2901
+ The former is simply more terse and familiar. @xref {Ref.Expr.Copy },
2902
+ @xref {Ref.Expr.Move }.
2903
+
2904
+
2818
2905
@node Ref.Expr.Call
2819
2906
@subsection Ref.Expr.Call
2820
2907
@c * Ref.Expr.Call:: Expression for calling a function.
2821
2908
@cindex Call expression
2822
2909
@cindex Function calls
2823
2910
2824
2911
A @dfn {call expression } invokes a function, providing a tuple of input slots
2825
- and an reference slot to serve as the function's output, bound to the @var { lval }
2826
- on the right hand side of the call. If the function eventually returns, then
2827
- the expression completes.
2912
+ and a reference slot to serve as the function's output, bound to the
2913
+ @var { lval } on the right hand side of the call. If the function eventually
2914
+ returns, then the expression completes.
2828
2915
2829
2916
A call expression statically requires that the precondition declared in the
2830
2917
callee's signature is satisfied by the expression prestate. In this way,
@@ -3222,7 +3309,7 @@ fn main() @{
3222
3309
choose_player(r)
3223
3310
@}
3224
3311
@{ player: p, options: @{ size: "small", _@} , _@} @{
3225
- log p + " is small";
3312
+ log(info, p + " is small") ;
3226
3313
@}
3227
3314
_ @{
3228
3315
next_player();
@@ -3445,7 +3532,7 @@ expressions can be enabled per module.
3445
3532
3446
3533
Logging output is enabled by setting the @code {RUST_LOG } environment variable.
3447
3534
@code {RUST_LOG } accepts a logging specification that is a comma-separated list
3448
- of paths. For each module containing log statements , if @code {RUST_LOG }
3535
+ of paths. For each module containing log expressions , if @code {RUST_LOG }
3449
3536
contains the path to that module or a parent of that module, then its logs
3450
3537
will be output to the console. The path to an module consists of the crate
3451
3538
name, any parent modules, then the module itself, all separated by double
@@ -3457,13 +3544,14 @@ As an example, to see all the logs generated by the compiler, you would set
3457
3544
just crate resolution, you would set it to @code {rustc::metadata::creader }.
3458
3545
3459
3546
Note that when compiling either .rs or .rc files that don't specifiy a crate
3460
- name the crate is given a default name that matches the source file, sans
3461
- extension. In that case, to turn on logging for a program compiled from, e.g.
3462
- helloworld.rs, @code {RUST_LOG } should be set to @code {helloworld }.
3547
+ name the crate is given a default name that matches the source file, with the
3548
+ extension removed. In that case, to turn on logging for a program compiled
3549
+ from, e.g. @code {helloworld.rs }, @code {RUST_LOG } should be set to
3550
+ @code {helloworld }.
3463
3551
3464
3552
As a convenience, the logging spec can also be set to a special psuedo-crate,
3465
3553
@code {::help }. In this case, when the application starts, the runtime will
3466
- simply output a list of loaded modules containing log statements , then exit.
3554
+ simply output a list of loaded modules containing log expressions , then exit.
3467
3555
3468
3556
The Rust runtime itself generates logging information. The runtime's logs are
3469
3557
generated for a number of artificial modules in the @code {::rt } psuedo-crate,
0 commit comments