-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_rust
25 lines (22 loc) · 1.66 KB
/
my_rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Things I've come across and found useful:
* Shareable mutable containers.
Rust memory safety is based on this rule: Given an object `T`, it is only possible to
have one of the following:
- Having several immutable references (`&T`) to the object (also known as **aliasing**).
- Having one mutable reference (`&mut T`) to the object (also known as **mutability**).
This is enforced by the Rust compiler. However, there are situations where this rule is not
flexible enough. Sometimes it is required to have multiple references to an object and yet
mutate it.
If you create a safe reference with lifetime `'a` (either a `&T` or `&mut T` reference), then
you must not access the data in any way that contradicts that reference for the remainder of
`'a`. For example, this means that if you take the `*mut T` from an `UnsafeCell<T>` and cast it
to an `&T`, then the data in `T` must remain immutable (modulo any `UnsafeCell` data found
within `T`, of course) until that reference's lifetime expires. Similarly, if you create a `&mut
T` reference that is released to safe code, then you must not access the data within the
`UnsafeCell` until that reference expires.
For both `&T` without `UnsafeCell<_>` and `&mut T`, you must also not deallocate the data
until the reference expires. As a special exception, given an `&T`, any part of it that is
inside an `UnsafeCell<_>` may be deallocated during the lifetime of the reference, after the
last time the reference is used (dereferenced or reborrowed). Since you cannot deallocate a part
of what a reference points to, this means the memory an `&T` points to can be deallocted only if
*every part of it* (including padding) is inside an `UnsafeCell`.