Skip to content

Commit 2d06415

Browse files
committed
add docs for non defining vs defining uses
1 parent f5c9735 commit 2d06415

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/solve/opaque-types.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,39 @@ This should be a self-contained explanation of the behavior in the new solver.
55

66
[opaque types]: ../opaque-types-type-alias-impl-trait.md
77

8+
## Non-Defining vs Defining Uses
9+
10+
The distinction between non-defining and defining uses determines the inference behavior and governs the strictness of the abstraction. A defining use reveals the underlying hidden type, while a non-defining use enforces rigidity, treating the type as an abstract placeholder.
11+
12+
### Non-defining Use
13+
14+
This concept is what makes an opaque type rigid. By treating the underlying type as unknown, the solver maintains abstraction.
15+
16+
```rust
17+
fn foo() -> impl Copy {
18+
let x: u32 = 56;
19+
x
20+
}
21+
22+
fn bar() {
23+
let x = foo();
24+
// Even though we know 'foo' returns a u32.The solver enforces
25+
// rigidity here and is a NON-DEFINING use.
26+
}
27+
```
28+
29+
### Defining Use
30+
31+
This is where the opaque type is actually defined. A defining use tells the compiler exactly what the concrete type is behind the abstraction.
32+
33+
```rust
34+
fn foo() -> impl Copy {
35+
let x: u32 = 56;
36+
// The return value x defines the hidden type as u32 and is a DEFINING use.
37+
x
38+
}
39+
```
40+
841
## opaques are alias types
942

1043
Opaque types are treated the same as other aliases, most notabily associated types,

0 commit comments

Comments
 (0)