Skip to content

Commit da706b0

Browse files
committed
---
yaml --- r: 30254 b: refs/heads/incoming c: 4fc164a h: refs/heads/master v: v3
1 parent b50f6d7 commit da706b0

File tree

2 files changed

+1
-93
lines changed

2 files changed

+1
-93
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
9-
refs/heads/incoming: df34fe917a6dd8b4696a36bf4e4e3e162ca97607
9+
refs/heads/incoming: 4fc164a549fc342401d9d935c0e663af43d88e4b
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/doc/tutorial.md

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,98 +1547,6 @@ fn contains(v: ~[int], elt: int) -> bool {
15471547

15481548
`for` syntax only works with stack closures.
15491549

1550-
# Argument passing
1551-
1552-
Rust datatypes are not trivial to copy (the way, for example,
1553-
JavaScript values can be copied by simply taking one or two machine
1554-
words and plunking them somewhere else). Shared boxes require
1555-
reference count updates, and big records, enums, or unique pointers require
1556-
an arbitrary amount of data to be copied (plus updating the reference
1557-
counts of shared boxes hanging off them).
1558-
1559-
For this reason, the default calling convention for Rust functions
1560-
leaves ownership of the arguments with the caller. The caller
1561-
guarantees that the arguments will outlive the call, the callee merely
1562-
gets access to them.
1563-
1564-
## Safe references
1565-
1566-
*This system has recently changed. An explanation is forthcoming.*
1567-
1568-
## Other uses of safe references
1569-
1570-
Safe references are not only used for argument passing. When you
1571-
destructure on a value in a `match` expression, or loop over a vector
1572-
with `for`, variables bound to the inside of the given data structure
1573-
will use safe references, not copies. This means such references are
1574-
very cheap, but you'll occasionally have to copy them to ensure
1575-
safety.
1576-
1577-
~~~~
1578-
let mut my_rec = {a: 4, b: ~[1, 2, 3]};
1579-
match my_rec {
1580-
{a, b} => {
1581-
log(info, b); // This is okay
1582-
my_rec = {a: a + 1, b: b + ~[a]};
1583-
log(info, b); // Here reference b has become invalid
1584-
}
1585-
}
1586-
~~~~
1587-
1588-
It's unsafe to dereference `b` in the second `log` expression, because `b` is
1589-
a _pointer_ to the inside of `my_rec`, and the assignment statement has
1590-
allocated a new record and assigned `my_rec` to point to it. Thus, the old
1591-
contents of `my_rec` are no longer live, and `b` is dangling at this point.
1592-
The borrow-checking analysis inside the compiler recognizes this situation
1593-
and rejects the program.
1594-
1595-
## Argument passing styles
1596-
1597-
The fact that arguments are conceptually passed by safe reference does
1598-
not mean all arguments are passed by pointer. Composite types like
1599-
records and enums *are* passed by pointer, but single-word values, like
1600-
integers and pointers, are simply passed by value. Most of the time,
1601-
the programmer does not have to worry about this, as the compiler will
1602-
simply pick the most efficient passing style. There is one exception,
1603-
which will be described in the section on [generics](#generics).
1604-
1605-
To explicitly set the passing-style for a parameter, you prefix the
1606-
argument name with a sigil. There are three special passing styles that
1607-
are often useful. The first is by-mutable-pointer, written with a
1608-
single `&`:
1609-
1610-
~~~~
1611-
fn vec_push(&v: ~[int], elt: int) {
1612-
v += ~[elt];
1613-
}
1614-
~~~~
1615-
1616-
This allows the function to mutate the value of the argument, *in the
1617-
caller's context*. Clearly, you are only allowed to pass things that
1618-
can actually be mutated to such a function.
1619-
1620-
Then there is the by-copy style, written `+`. This indicates that the
1621-
function wants to take ownership of the argument value. If the caller
1622-
does not use the argument after the call, it will be 'given' to the
1623-
callee. Otherwise a copy will be made. This mode is mostly used for
1624-
functions that construct data structures. The argument will end up
1625-
being owned by the data structure, so if that can be done without a
1626-
copy, that's a win.
1627-
1628-
~~~~
1629-
type person = {name: ~str, address: ~str};
1630-
fn make_person(+name: ~str, +address: ~str) -> person {
1631-
return {name: name, address: address};
1632-
}
1633-
~~~~
1634-
1635-
Finally there is by-move style, written `-`. This indicates that the
1636-
function will take ownership of the argument, like with by-copy style,
1637-
but a copy must not be made. The caller is (statically) obliged to not
1638-
use the argument after the call; it is de-initialized as part of the
1639-
call. This is used to support ownership-passing in the presence of
1640-
non-copyable types.
1641-
16421550
# Generics
16431551

16441552
## Generic functions

0 commit comments

Comments
 (0)