You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letshape_copy=sh; // doesn't move sh because T: Copy
72
-
draw_twice(surface, sh); // Can use generic function because T: Shape
73
-
}
63
+
r[bound.satisfaction.bounds]
74
64
75
-
structFigure<S:Shape>(S, S);
65
+
While inside of a generic item, trait bounds can be satisfied by using the where-bounds of the current item as the item is able to assume that its bounds are satisfied. For this, the where-bound is then equated with the trait bound that needs to be satisfied.
76
66
77
-
fnname_figure<U:Shape>(
78
-
figure:Figure<U>, // Type Figure<U> is well-formed because U: Shape
79
-
) {
80
-
println!(
81
-
"Figure of two {}",
82
-
U::name(), // Can use associated function
83
-
);
67
+
r[bound.satisfaction.candidate-preference]
68
+
69
+
> This is purely descriptive. Candidate preference behavior may change in future releases and must not be relied upon for correctness or soundness.
70
+
71
+
If there are multiple ways to satisfy a trait bound, some groups of candidate are preferred over others. In case a single group has multiple different candidates, the bound remains ambiguous. Candidate preference has the following order
72
+
- builtin implementations of `Sized`
73
+
- if there are any non-global where-bounds, all where-bounds
74
+
- impls
75
+
- global where-bounds
76
+
77
+
```rust
78
+
structContainer<T> {
79
+
inner:Vec<T>,
80
+
}
81
+
impl<T:Clone> forContainer<T> {
82
+
fnclone(&self) ->Self {
83
+
Container { inner:self.inner.clone() }
84
+
}
84
85
}
85
-
```
86
86
87
-
r[bound.trivial]
88
-
Bounds that don't use the item's parameters or [higher-ranked lifetimes] are checked when the item is defined.
89
-
It is an error for such a bound to be false.
90
-
91
-
r[bound.special]
92
-
[`Copy`], [`Clone`], and [`Sized`] bounds are also checked for certain generic types when using the item, even if the use does not provide a concrete type.
93
-
It is an error to have `Copy` or `Clone` as a bound on a mutable reference, [trait object], or [slice].
94
-
It is an error to have `Sized` as a bound on a trait object or slice.
95
-
96
-
```rust,compile_fail
97
-
struct A<'a, T>
98
-
where
99
-
i32: Default, // Allowed, but not useful
100
-
i32: Iterator, // Error: `i32` is not an iterator
101
-
&'a mut T: Copy, // (at use) Error: the trait bound is not satisfied
102
-
[T]: Sized, // (at use) Error: size cannot be known at compilation
103
-
{
104
-
f: &'a T,
87
+
fnis_clone<T:Clone>() {}
88
+
fngeneric_fn<T:Clone>() {
89
+
// `Container<T>: Clone` is satisfied via the impl above. This requires
90
+
// the nested trait bound `T: Clone` which is satisfied via the where-bound.
0 commit comments