Skip to content

Commit 90ae3e3

Browse files
committed
Document copy/move/swap/assign expressions more accurately. Fix up some drift on log docs.
1 parent 468ced3 commit 90ae3e3

File tree

2 files changed

+121
-33
lines changed

2 files changed

+121
-33
lines changed

doc/rust.texi

Lines changed: 120 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,12 +1701,14 @@ import std::option::*;
17011701
import std::str::@{char_at, hash@};
17021702
17031703
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")));
17101712
@}
17111713
@end example
17121714

@@ -1801,7 +1803,7 @@ A special kind of function can be declared with a @code{!} character where the
18011803
output slot type would normally be. For example:
18021804
@example
18031805
fn my_err(s: str) -> ! @{
1804-
log s;
1806+
log(info, s);
18051807
fail;
18061808
@}
18071809
@end example
@@ -2767,6 +2769,9 @@ effects of the expression's evaluation.
27672769

27682770
@menu
27692771
* 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.
27702775
* Ref.Expr.Call:: Expression for calling a function.
27712776
* Ref.Expr.Bind:: Expression for binding arguments to functions.
27722777
* Ref.Expr.Ret:: Expression for stopping and producing a value.
@@ -2788,43 +2793,125 @@ effects of the expression's evaluation.
27882793
* Ref.Expr.AnonObj:: Expression for extending objects with additional methods.
27892794
@end menu
27902795

2791-
27922796
@node Ref.Expr.Copy
27932797
@subsection Ref.Expr.Copy
27942798
@c * Ref.Expr.Copy:: Expression for copying a value.
27952799
@cindex Copy expression
2796-
@cindex Assignment operator, see @i{Copy expression}
2800+
@cindex Copy operator, see @i{Move expression}
27972801

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.
28002804

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.
28042808

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.
28102811

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:
28122817
@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
28162827
@end example
28172828

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+
28182905
@node Ref.Expr.Call
28192906
@subsection Ref.Expr.Call
28202907
@c * Ref.Expr.Call:: Expression for calling a function.
28212908
@cindex Call expression
28222909
@cindex Function calls
28232910

28242911
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.
28282915

28292916
A call expression statically requires that the precondition declared in the
28302917
callee's signature is satisfied by the expression prestate. In this way,
@@ -3222,7 +3309,7 @@ fn main() @{
32223309
choose_player(r)
32233310
@}
32243311
@{player: p, options: @{size: "small", _@}, _@} @{
3225-
log p + " is small";
3312+
log(info, p + " is small");
32263313
@}
32273314
_ @{
32283315
next_player();
@@ -3445,7 +3532,7 @@ expressions can be enabled per module.
34453532

34463533
Logging output is enabled by setting the @code{RUST_LOG} environment variable.
34473534
@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}
34493536
contains the path to that module or a parent of that module, then its logs
34503537
will be output to the console. The path to an module consists of the crate
34513538
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
34573544
just crate resolution, you would set it to @code{rustc::metadata::creader}.
34583545

34593546
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}.
34633551

34643552
As a convenience, the logging spec can also be set to a special psuedo-crate,
34653553
@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.
34673555

34683556
The Rust runtime itself generates logging information. The runtime's logs are
34693557
generated for a number of artificial modules in the @code{::rt} psuedo-crate,

mk/docs.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ GENERATED += nd/$(1)/Languages.txt \
5252
nd/$(1)/Menu.txt \
5353
nd/$(1)/Data
5454

55-
DOCS += nd/$(1)/index.html nd/$(1)/lib.css
55+
DOCS += doc/$(1)/index.html nd/$(1)/lib.css
5656

5757
endef
5858

0 commit comments

Comments
 (0)