|
1.
|
|
|
# Introduction
|
|
|
type: Plain text
|
|
|
|
(no translation yet)
|
|
|
|
Located in
src/doc/guide-ffi.md:4 src/doc/guide-lifetimes.md:4
src/doc/guide-macros.md:13 src/doc/guide-plugin.md:28
src/doc/guide-tasks.md:4 src/doc/guide-unsafe.md:4
|
|
2.
|
|
|
% The Rust References and Lifetimes Guide
|
|
|
type: Plain text
|
|
|
|
(no translation yet)
|
|
|
|
Located in
src/doc/guide-lifetimes.md:2
|
|
3.
|
|
|
References are one of the more flexible and powerful tools available in Rust. They can point anywhere: into the heap, stack, and even into the interior of another data structure. A reference is as flexible as a C pointer or C++ reference.
|
|
|
type: Plain text
|
|
|
|
(no translation yet)
|
|
|
|
Located in
src/doc/guide-lifetimes.md:9
|
|
4.
|
|
|
Unlike C and C++ compilers, the Rust compiler includes special static checks that ensure that programs use references safely.
|
|
|
type: Plain text
|
|
|
|
(no translation yet)
|
|
|
|
Located in
src/doc/guide-lifetimes.md:12
|
|
5.
|
|
|
Despite their complete safety, a reference's representation at runtime is the same as that of an ordinary pointer in a C program. They introduce zero overhead. The compiler does all safety checks at compile time.
|
|
|
type: Plain text
|
|
|
|
(no translation yet)
|
|
|
|
Located in
src/doc/guide-lifetimes.md:16
|
|
6.
|
|
|
Although references have rather elaborate theoretical underpinnings (e.g. region pointers), the core concepts will be familiar to anyone who has worked with C or C++. The best way to explain how they are used—and their limitations—is probably just to work through several examples.
|
|
|
type: Plain text
|
|
|
|
(no translation yet)
|
|
|
|
Located in
src/doc/guide-lifetimes.md:21
|
|
7.
|
|
|
# By example
|
|
|
type: Plain text
|
|
|
|
(no translation yet)
|
|
|
|
Located in
src/doc/guide-lifetimes.md:23
|
|
8.
|
|
|
References, sometimes known as *borrowed pointers*, are only valid for a limited duration. References never claim any kind of ownership over the data that they point to. Instead, they are used for cases where you would like to use data for a short time.
|
|
|
type: Plain text
|
|
|
|
(no translation yet)
|
|
|
|
Located in
src/doc/guide-lifetimes.md:28
|
|
9.
|
|
|
Consider a simple struct type `Point`:
|
|
|
type: Plain text
|
|
|
|
(no translation yet)
|
|
|
|
Located in
src/doc/guide-lifetimes.md:30
|
|
10.
|
|
|
~~~ struct Point {x: f64, y: f64} ~~~
|
|
|
type: Plain text
|
|
|
|
(no translation yet)
|
|
|
|
Located in
src/doc/guide-lifetimes.md:34
|