@@ -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