Skip to content

Commit

Permalink
tutorial: Add some work on borrowed pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Jul 7, 2012
1 parent af199f2 commit 28fec95
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,33 @@ to other tasks. The sending task will give up ownership of the box,
and won't be able to access it afterwards. The receiving task will
become the sole owner of the box.
### Borrowed Pointers
Rust borrowed pointers are a general purpose reference/pointer type,
similar to the C++ reference type, but guaranteed to point to valid
memory. In contrast to unique pointers, where the holder of a unique
pointer is the owner of the pointed-to memory, borrowed pointers never
imply ownership. Pointers may be borrowed from any type, in which case
the pointer is guaranteed not to outlive the value it points to.
~~~~
# fn work_with_foo_by_pointer(f: &str) { }
let foo = "foo";
work_with_foo_by_pointer(&foo);
~~~~
The following shows an example of what is _not_ possible with borrowed
pointers. If you were able to write this then the pointer to `foo`
would outlive `foo` itself.
~~~~ {.ignore}
let foo_ptr;
{
let foo = "foo";
foo_ptr = &foo;
}
~~~~
### Mutability
All pointer types have a mutable variant, written `@mut T` or `~mut
Expand Down

0 comments on commit 28fec95

Please sign in to comment.