Skip to content

Commit 90273c1

Browse files
committed
---
yaml --- r: 31119 b: refs/heads/incoming c: a46db48 h: refs/heads/master i: 31117: 4066a4e 31115: e89bb31 31111: 1f5ec07 31103: 659523d v: v3
1 parent 29c6c0c commit 90273c1

File tree

3 files changed

+103
-105
lines changed

3 files changed

+103
-105
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: c765c59ab19f7dcb2f9c5e90d5ea4e724df33904
9+
refs/heads/incoming: a46db484abcdd517c0116301c77364e8e1c373af
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/doc/tutorial-borrowed-ptr.md

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@
22

33
# Introduction
44

5-
Borrowed pointers are one of the more flexible and powerful tools
6-
available in Rust. A borrowed pointer can be used to point anywhere:
7-
into the managed and exchange heaps, into the stack, and even into the
8-
interior of another data structure. With regard to flexibility, it is
9-
comparable to a C pointer or C++ reference. However, unlike C and C++,
10-
the Rust compiler includes special checks that ensure that borrowed
11-
pointers are being used safely. Another advantage of borrowed pointers
12-
is that they are invisible to the garbage collector, so working with
13-
borrowed pointers helps keep things efficient.
14-
15-
Despite the fact that they are completely safe, at runtime, a borrowed
16-
pointer is “just a pointer”. They introduce zero overhead. All safety
17-
checks are done at compilation time.
5+
Borrowed pointers are one of the more flexible and powerful tools available in
6+
Rust. A borrowed pointer can point anywhere: into the managed or exchange
7+
heap, into the stack, and even into the interior of another data structure. A
8+
borrowed pointer is as flexible as a C pointer or C++ reference. However,
9+
unlike C and C++ compilers, the Rust compiler includes special static checks
10+
that ensure that programs use borrowed pointers safely. Another advantage of
11+
borrowed pointers is that they are invisible to the garbage collector, so
12+
working with borrowed pointers helps reduce the overhead of automatic memory
13+
management.
14+
15+
Despite their complete safety, a borrowed pointer's representation at runtime
16+
is the same as that of an ordinary pointer in a C program. They introduce zero
17+
overhead. The compiler does all safety checks at compile time.
1818

1919
Although borrowed pointers have rather elaborate theoretical
2020
underpinnings (region pointers), the core concepts will be familiar to
21-
anyone who worked with C or C++. Therefore, the best way to explain
21+
anyone who has worked with C or C++. Therefore, the best way to explain
2222
how they are used—and their limitations—is probably just to work
2323
through several examples.
2424

2525
# By example
2626

27-
Borrowed pointers are called borrowed because they are only valid for
28-
a limit duration. Borrowed pointers never claim any kind of ownership
29-
over the data that they point at: instead, they are used for cases
30-
where you like to make use of data for a short time.
27+
Borrowed pointers are called *borrowed* because they are only valid for
28+
a limited duration. Borrowed pointers never claim any kind of ownership
29+
over the data that they point to: instead, they are used for cases
30+
where you would like to use data for a short time.
3131

3232
As an example, consider a simple struct type `Point`:
3333

3434
~~~
3535
struct Point {x: float, y: float}
3636
~~~
3737

38-
We can use this simple definition to allocate points in many ways. For
38+
We can use this simple definition to allocate points in many different ways. For
3939
example, in this code, each of these three local variables contains a
4040
point, but allocated in a different place:
4141

@@ -46,17 +46,17 @@ let shared_box : @Point = @Point {x: 5.0, y: 1.0};
4646
let unique_box : ~Point = ~Point {x: 7.0, y: 9.0};
4747
~~~
4848

49-
Suppose we wanted to write a procedure that computed the distance
50-
between any two points, no matter where they were stored. For example,
51-
we might like to compute the distance between `on_the_stack` and
52-
`shared_box`, or between `shared_box` and `unique_box`. One option is
53-
to define a function that takes two arguments of type point—that is,
54-
it takes the points by value. But this will cause the points to be
55-
copied when we call the function. For points, this is probably not so
56-
bad, but often copies are expensive or, worse, if there are mutable
57-
fields, they can change the semantics of your program. So wed like to
58-
define a function that takes the points by pointer. We can use
59-
borrowed pointers to do this:
49+
Suppose we wanted to write a procedure that computed the distance between any
50+
two points, no matter where they were stored. For example, we might like to
51+
compute the distance between `on_the_stack` and `shared_box`, or between
52+
`shared_box` and `unique_box`. One option is to define a function that takes
53+
two arguments of type `Point`—that is, it takes the points by value. But we
54+
define it this way, calling the function will cause the points to be
55+
copied. For points, this is probably not so bad, but often copies are
56+
expensive. Worse, if the data type contains mutable fields, copying can change
57+
the semantics of your program in unexpected ways. So we'd like to define a
58+
function that takes the points by pointer. We can use borrowed pointers to do
59+
this:
6060

6161
~~~
6262
# struct Point {x: float, y: float}
@@ -80,28 +80,28 @@ compute_distance(&on_the_stack, shared_box);
8080
compute_distance(shared_box, unique_box);
8181
~~~
8282

83-
Here the `&` operator is used to take the address of the variable
83+
Here, the `&` operator takes the address of the variable
8484
`on_the_stack`; this is because `on_the_stack` has the type `Point`
8585
(that is, a struct value) and we have to take its address to get a
8686
value. We also call this _borrowing_ the local variable
87-
`on_the_stack`, because we are created an alias: that is, another
88-
route to the same data.
89-
90-
In the case of the boxes `shared_box` and `unique_box`, however, no
91-
explicit action is necessary. The compiler will automatically convert
92-
a box like `@Point` or `~Point` to a borrowed pointer like
93-
`&Point`. This is another form of borrowing; in this case, the
94-
contents of the shared/unique box is being lent out.
95-
96-
Whenever a value is borrowed, there are some limitations on what you
97-
can do with the original. For example, if the contents of a variable
98-
have been lent out, you cannot send that variable to another task, nor
99-
will you be permitted to take actions that might cause the borrowed
100-
value to be freed or to change its type (I’ll get into what kinds of
101-
actions those are shortly). This rule should make intuitive sense: you
102-
must wait for a borrowed value to be returned (that is, for the
103-
borrowed pointer to go out of scope) before you can make full use of
104-
it again.
87+
`on_the_stack`, because we have created an alias: that is, another
88+
name for the same data.
89+
90+
In contrast, we can pass the boxes `shared_box` and `unique_box` to
91+
`compute_distance` directly. The compiler automatically converts a box like
92+
`@Point` or `~Point` to a borrowed pointer like `&Point`. This is another form
93+
of borrowing: in this case, the caller lends the contents of the shared or
94+
unique box to the callee.
95+
96+
Whenever a caller lends data to a callee, there are some limitations on what
97+
the caller can do with the original. For example, if the contents of a
98+
variable have been lent out, you cannot send that variable to another task. In
99+
addition, the compiler will reject any code that might cause the borrowed
100+
value to be freed or overwrite its component fields with values of different
101+
types (I'll get into what kinds of actions those are shortly). This rule
102+
should make intuitive sense: you must wait for a borrower to return the value
103+
that you lent it (that is, wait for the borrowed pointer to go out of scope)
104+
before you can make full use of it again.
105105

106106
# Other uses for the & operator
107107

branches/incoming/src/libcore/str.rs

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ pub pure fn is_whitespace(s: &str) -> bool {
13611361
*
13621362
* Alphanumeric characters are determined by `char::is_alphanumeric`
13631363
*/
1364-
pure fn is_alphanumeric(s: &str) -> bool {
1364+
fn is_alphanumeric(s: &str) -> bool {
13651365
return all(s, char::is_alphanumeric);
13661366
}
13671367

@@ -2030,22 +2030,22 @@ pub mod raw {
20302030
}
20312031

20322032
pub trait UniqueStr {
2033-
pure fn trim() -> self;
2034-
pure fn trim_left() -> self;
2035-
pure fn trim_right() -> self;
2033+
fn trim() -> self;
2034+
fn trim_left() -> self;
2035+
fn trim_right() -> self;
20362036
}
20372037

20382038
/// Extension methods for strings
20392039
impl ~str: UniqueStr {
20402040
/// Returns a string with leading and trailing whitespace removed
20412041
#[inline]
2042-
pure fn trim() -> ~str { trim(self) }
2042+
fn trim() -> ~str { trim(self) }
20432043
/// Returns a string with leading whitespace removed
20442044
#[inline]
2045-
pure fn trim_left() -> ~str { trim_left(self) }
2045+
fn trim_left() -> ~str { trim_left(self) }
20462046
/// Returns a string with trailing whitespace removed
20472047
#[inline]
2048-
pure fn trim_right() -> ~str { trim_right(self) }
2048+
fn trim_right() -> ~str { trim_right(self) }
20492049
}
20502050

20512051
#[cfg(notest)]
@@ -2062,33 +2062,33 @@ pub mod traits {
20622062
pub mod traits {}
20632063

20642064
pub trait StrSlice {
2065-
pure fn all(it: fn(char) -> bool) -> bool;
2066-
pure fn any(it: fn(char) -> bool) -> bool;
2067-
pure fn contains(needle: &a/str) -> bool;
2068-
pure fn contains_char(needle: char) -> bool;
2069-
pure fn each(it: fn(u8) -> bool);
2070-
pure fn eachi(it: fn(uint, u8) -> bool);
2071-
pure fn each_char(it: fn(char) -> bool);
2072-
pure fn each_chari(it: fn(uint, char) -> bool);
2073-
pure fn ends_with(needle: &str) -> bool;
2074-
pure fn is_empty() -> bool;
2075-
pure fn is_not_empty() -> bool;
2076-
pure fn is_whitespace() -> bool;
2077-
pure fn is_alphanumeric() -> bool;
2065+
fn all(it: fn(char) -> bool) -> bool;
2066+
fn any(it: fn(char) -> bool) -> bool;
2067+
fn contains(needle: &a/str) -> bool;
2068+
fn contains_char(needle: char) -> bool;
2069+
fn each(it: fn(u8) -> bool);
2070+
fn eachi(it: fn(uint, u8) -> bool);
2071+
fn each_char(it: fn(char) -> bool);
2072+
fn each_chari(it: fn(uint, char) -> bool);
2073+
fn ends_with(needle: &str) -> bool;
2074+
fn is_empty() -> bool;
2075+
fn is_not_empty() -> bool;
2076+
fn is_whitespace() -> bool;
2077+
fn is_alphanumeric() -> bool;
20782078
pure fn len() -> uint;
20792079
pure fn slice(begin: uint, end: uint) -> ~str;
2080-
pure fn split(sepfn: fn(char) -> bool) -> ~[~str];
2081-
pure fn split_char(sep: char) -> ~[~str];
2082-
pure fn split_str(sep: &a/str) -> ~[~str];
2083-
pure fn starts_with(needle: &a/str) -> bool;
2084-
pure fn substr(begin: uint, n: uint) -> ~str;
2080+
fn split(sepfn: fn(char) -> bool) -> ~[~str];
2081+
fn split_char(sep: char) -> ~[~str];
2082+
fn split_str(sep: &a/str) -> ~[~str];
2083+
fn starts_with(needle: &a/str) -> bool;
2084+
fn substr(begin: uint, n: uint) -> ~str;
20852085
pure fn to_lower() -> ~str;
20862086
pure fn to_upper() -> ~str;
2087-
pure fn escape_default() -> ~str;
2088-
pure fn escape_unicode() -> ~str;
2089-
pure fn trim() -> ~str;
2090-
pure fn trim_left() -> ~str;
2091-
pure fn trim_right() -> ~str;
2087+
fn escape_default() -> ~str;
2088+
fn escape_unicode() -> ~str;
2089+
fn trim() -> ~str;
2090+
fn trim_left() -> ~str;
2091+
fn trim_right() -> ~str;
20922092
pure fn to_unique() -> ~str;
20932093
pure fn char_at(i: uint) -> char;
20942094
}
@@ -2100,56 +2100,54 @@ impl &str: StrSlice {
21002100
* contains no characters
21012101
*/
21022102
#[inline]
2103-
pure fn all(it: fn(char) -> bool) -> bool { all(self, it) }
2103+
fn all(it: fn(char) -> bool) -> bool { all(self, it) }
21042104
/**
21052105
* Return true if a predicate matches any character (and false if it
21062106
* matches none or there are no characters)
21072107
*/
21082108
#[inline]
2109-
pure fn any(it: fn(char) -> bool) -> bool { any(self, it) }
2109+
fn any(it: fn(char) -> bool) -> bool { any(self, it) }
21102110
/// Returns true if one string contains another
21112111
#[inline]
2112-
pure fn contains(needle: &a/str) -> bool { contains(self, needle) }
2112+
fn contains(needle: &a/str) -> bool { contains(self, needle) }
21132113
/// Returns true if a string contains a char
21142114
#[inline]
2115-
pure fn contains_char(needle: char) -> bool {
2116-
contains_char(self, needle)
2117-
}
2115+
fn contains_char(needle: char) -> bool { contains_char(self, needle) }
21182116
/// Iterate over the bytes in a string
21192117
#[inline]
2120-
pure fn each(it: fn(u8) -> bool) { each(self, it) }
2118+
fn each(it: fn(u8) -> bool) { each(self, it) }
21212119
/// Iterate over the bytes in a string, with indices
21222120
#[inline]
2123-
pure fn eachi(it: fn(uint, u8) -> bool) { eachi(self, it) }
2121+
fn eachi(it: fn(uint, u8) -> bool) { eachi(self, it) }
21242122
/// Iterate over the chars in a string
21252123
#[inline]
2126-
pure fn each_char(it: fn(char) -> bool) { each_char(self, it) }
2124+
fn each_char(it: fn(char) -> bool) { each_char(self, it) }
21272125
/// Iterate over the chars in a string, with indices
21282126
#[inline]
2129-
pure fn each_chari(it: fn(uint, char) -> bool) { each_chari(self, it) }
2127+
fn each_chari(it: fn(uint, char) -> bool) { each_chari(self, it) }
21302128
/// Returns true if one string ends with another
21312129
#[inline]
2132-
pure fn ends_with(needle: &str) -> bool { ends_with(self, needle) }
2130+
fn ends_with(needle: &str) -> bool { ends_with(self, needle) }
21332131
/// Returns true if the string has length 0
21342132
#[inline]
2135-
pure fn is_empty() -> bool { is_empty(self) }
2133+
fn is_empty() -> bool { is_empty(self) }
21362134
/// Returns true if the string has length greater than 0
21372135
#[inline]
2138-
pure fn is_not_empty() -> bool { is_not_empty(self) }
2136+
fn is_not_empty() -> bool { is_not_empty(self) }
21392137
/**
21402138
* Returns true if the string contains only whitespace
21412139
*
21422140
* Whitespace characters are determined by `char::is_whitespace`
21432141
*/
21442142
#[inline]
2145-
pure fn is_whitespace() -> bool { is_whitespace(self) }
2143+
fn is_whitespace() -> bool { is_whitespace(self) }
21462144
/**
21472145
* Returns true if the string contains only alphanumerics
21482146
*
21492147
* Alphanumeric characters are determined by `char::is_alphanumeric`
21502148
*/
21512149
#[inline]
2152-
pure fn is_alphanumeric() -> bool { is_alphanumeric(self) }
2150+
fn is_alphanumeric() -> bool { is_alphanumeric(self) }
21532151
#[inline]
21542152
/// Returns the size in bytes not counting the null terminator
21552153
pure fn len() -> uint { len(self) }
@@ -2164,29 +2162,29 @@ impl &str: StrSlice {
21642162
pure fn slice(begin: uint, end: uint) -> ~str { slice(self, begin, end) }
21652163
/// Splits a string into substrings using a character function
21662164
#[inline]
2167-
pure fn split(sepfn: fn(char) -> bool) -> ~[~str] { split(self, sepfn) }
2165+
fn split(sepfn: fn(char) -> bool) -> ~[~str] { split(self, sepfn) }
21682166
/**
21692167
* Splits a string into substrings at each occurrence of a given character
21702168
*/
21712169
#[inline]
2172-
pure fn split_char(sep: char) -> ~[~str] { split_char(self, sep) }
2170+
fn split_char(sep: char) -> ~[~str] { split_char(self, sep) }
21732171
/**
21742172
* Splits a string into a vector of the substrings separated by a given
21752173
* string
21762174
*/
21772175
#[inline]
2178-
pure fn split_str(sep: &a/str) -> ~[~str] { split_str(self, sep) }
2176+
fn split_str(sep: &a/str) -> ~[~str] { split_str(self, sep) }
21792177
/// Returns true if one string starts with another
21802178
#[inline]
2181-
pure fn starts_with(needle: &a/str) -> bool { starts_with(self, needle) }
2179+
fn starts_with(needle: &a/str) -> bool { starts_with(self, needle) }
21822180
/**
21832181
* Take a substring of another.
21842182
*
21852183
* Returns a string containing `n` characters starting at byte offset
21862184
* `begin`.
21872185
*/
21882186
#[inline]
2189-
pure fn substr(begin: uint, n: uint) -> ~str { substr(self, begin, n) }
2187+
fn substr(begin: uint, n: uint) -> ~str { substr(self, begin, n) }
21902188
/// Convert a string to lowercase
21912189
#[inline]
21922190
pure fn to_lower() -> ~str { to_lower(self) }
@@ -2195,20 +2193,20 @@ impl &str: StrSlice {
21952193
pure fn to_upper() -> ~str { to_upper(self) }
21962194
/// Escape each char in `s` with char::escape_default.
21972195
#[inline]
2198-
pure fn escape_default() -> ~str { escape_default(self) }
2196+
fn escape_default() -> ~str { escape_default(self) }
21992197
/// Escape each char in `s` with char::escape_unicode.
22002198
#[inline]
2201-
pure fn escape_unicode() -> ~str { escape_unicode(self) }
2199+
fn escape_unicode() -> ~str { escape_unicode(self) }
22022200

22032201
/// Returns a string with leading and trailing whitespace removed
22042202
#[inline]
2205-
pure fn trim() -> ~str { trim(self) }
2203+
fn trim() -> ~str { trim(self) }
22062204
/// Returns a string with leading whitespace removed
22072205
#[inline]
2208-
pure fn trim_left() -> ~str { trim_left(self) }
2206+
fn trim_left() -> ~str { trim_left(self) }
22092207
/// Returns a string with trailing whitespace removed
22102208
#[inline]
2211-
pure fn trim_right() -> ~str { trim_right(self) }
2209+
fn trim_right() -> ~str { trim_right(self) }
22122210

22132211
#[inline]
22142212
pure fn to_unique() -> ~str { self.slice(0, self.len()) }

0 commit comments

Comments
 (0)