File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -156,6 +156,35 @@ fn call_on_ref_zero<F>(f: F) where F: for<'a> Fn(&'a i32) {
156156}
157157```
158158
159+ ## Implied bounds
160+
161+ Rust sometimes infers some bounds the user would have otherwise been required to write.
162+
163+ ``` rust
164+ fn requires_t_outlives_a <'a , T >(x : & 'a T ) {}
165+ ```
166+ While this function requires ` t ` to outlive ` 'a ` , this is inferred as the function signature
167+ contains the type ` &'a T ` which is only valid if ` T: 'a ` holds.
168+
169+ Rust adds implied bounds for all inputs and outputs of functions. Inside of ` requires_t_outlives_a `
170+ you can assume ` T: 'a ` to hold even if you don't explicitly specify this:
171+ ``` rust
172+ fn requires_t_outlives_a <'a , T >(x : & 'a T ) {
173+ requires_t_outlives_a_not_implied :: <'a , T >();
174+ }
175+
176+ fn requires_t_outlives_a_not_implied <'a , T : 'a >() {}
177+ ```
178+
179+ Only lifetime bounds are implied, trait bounds still have to be explicitly added.
180+ This behavior may change in the future however. The following example still causes an error:
181+ ``` rust,compile_fail
182+ use std::fmt::Debug;
183+ struct IsDebug<T: Debug>(T);
184+ // error[E0277]: `T` doesn't implement `Debug`
185+ fn doesnt_specify_t_debug<T>(x: IsDebug<T>) {}
186+ ```
187+
159188[ LIFETIME_OR_LABEL ] : tokens.md#lifetimes-and-loop-labels
160189[ _GenericParams_ ] : items/generics.md
161190[ _TypePath_ ] : paths.md#paths-in-types
You can’t perform that action at this time.
0 commit comments