Skip to content

Commit 28fec95

Browse files
committed
tutorial: Add some work on borrowed pointers
1 parent af199f2 commit 28fec95

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

doc/tutorial.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,33 @@ to other tasks. The sending task will give up ownership of the box,
15571557
and won't be able to access it afterwards. The receiving task will
15581558
become the sole owner of the box.
15591559
1560+
### Borrowed Pointers
1561+
1562+
Rust borrowed pointers are a general purpose reference/pointer type,
1563+
similar to the C++ reference type, but guaranteed to point to valid
1564+
memory. In contrast to unique pointers, where the holder of a unique
1565+
pointer is the owner of the pointed-to memory, borrowed pointers never
1566+
imply ownership. Pointers may be borrowed from any type, in which case
1567+
the pointer is guaranteed not to outlive the value it points to.
1568+
1569+
~~~~
1570+
# fn work_with_foo_by_pointer(f: &str) { }
1571+
let foo = "foo";
1572+
work_with_foo_by_pointer(&foo);
1573+
~~~~
1574+
1575+
The following shows an example of what is _not_ possible with borrowed
1576+
pointers. If you were able to write this then the pointer to `foo`
1577+
would outlive `foo` itself.
1578+
1579+
~~~~ {.ignore}
1580+
let foo_ptr;
1581+
{
1582+
let foo = "foo";
1583+
foo_ptr = &foo;
1584+
}
1585+
~~~~
1586+
15601587
### Mutability
15611588
15621589
All pointer types have a mutable variant, written `@mut T` or `~mut

0 commit comments

Comments
 (0)