From 28fec95c5963bbd524f4271cb7cac63dcd2ec0cf Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 6 Jul 2012 18:25:39 -0700 Subject: [PATCH] tutorial: Add some work on borrowed pointers --- doc/tutorial.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/doc/tutorial.md b/doc/tutorial.md index 301ca6cf5cc..f72557216ad 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -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