Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/expressions/array-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ r[expr.array.index.trait]
For other types an index expression `a[b]` is equivalent to `*std::ops::Index::index(&a, b)`, or `*std::ops::IndexMut::index_mut(&mut a, b)` in a mutable place expression context.
Just as with methods, Rust will also insert dereference operations on `a` repeatedly to find an implementation.

> [!NOTE]
> The [temporary scopes] of `a` in `a[b]` and `*std::ops::Index::index(&a, b)` are not always the same in all immutable place expression contexts.
> Likewise, the temporary scopes of `a` in `a[b]` and `*std::ops::IndexMut::index_mut(&mut a, b)` are not always the same in all mutable place expression contexts.
>
> If `a[b]` has an [extended] temporary scope, then `a` does as well ([destructors.scope.lifetime-extension.sub-expressions]).
> However, the same does not apply to `*std::ops::Index::index(&a, b)` or `*std::ops::IndexMut::index_mut(&mut a, b)`.
>
> For example:
>
> ```rust
> // The temporary holding the result of `vec![()]` is extended to
> // live to the end of the block, so `x` may be used in subsequent
> // statements.
> let x = &vec![()][0];
> # x;
> ```
>
> ```rust,compile_fail,E0716
> // The temporary holding the result of `vec![()]` is dropped at the
> // end of the statement, so it's an error to use `y` after.
> let y = &*std::ops::Index::index(&vec![()], 0); // ERROR
> # y;
> ```

r[expr.array.index.zero-index]
Indices are zero-based for arrays and slices.

Expand Down Expand Up @@ -126,9 +150,11 @@ The array index expression can be implemented for types other than arrays and sl
[const block expression]: expr.block.const
[constant expression]: ../const_eval.md#constant-expressions
[constant item]: ../items/constant-items.md
[extended]: destructors.scope.lifetime-extension
[inferred const]: items.generics.const.inferred
[literal]: ../tokens.md#literals
[memory location]: ../expressions.md#place-expressions-and-value-expressions
[panic]: ../panic.md
[path]: path-expr.md
[slice]: ../types/slice.md
[temporary scopes]: destructors.scope.temporary
24 changes: 24 additions & 0 deletions src/expressions/operator-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,30 @@ let y = &mut 9;
assert_eq!(*y, 11);
```

> [!NOTE]
> The [temporary scopes][temporary scope] of `x` in `*x` and `*std::ops::Deref::deref(&x)` are not always the same in all immutable place expression contexts.
> Likewise, the temporary scopes of `x` in `*x` and `*std::ops::DerefMut::deref_mut(&mut x)` are not always the same in all mutable place expression contexts.
>
> If `*x` has an [extended][temporary lifetime extension] temporary scope, then `x` does as well ([destructors.scope.lifetime-extension.sub-expressions]).
> However, the same does not apply to `*std::ops::Deref::deref(&x)` or `*std::ops::DerefMut::deref_mut(&mut x)`.
>
> For example:
>
> ```rust
> // The temporary holding the result of `String::new()` is extended
> // to live to the end of the block, so `x` may be used in subsequent
> // statements.
> let x = &*String::new();
> # x;
> ```
>
> ```rust,compile_fail,E0716
> // The temporary holding the result of `String::new()` is dropped at
> // the end of the statement, so it's an error to use `y` after.
> let y = &*std::ops::Deref::deref(&String::new()); // ERROR
> # y;
> ```

r[expr.try]
## The try propagation expression

Expand Down