@@ -274,7 +274,7 @@ https://doc.rust-lang.org/reference.html#use-declarations
274
274
"## ,
275
275
276
276
E0401 : r##"
277
- Inner functions do not inherit type parameters from the functions they are
277
+ Inner items do not inherit type parameters from the functions they are
278
278
embedded in. For example, this will not compile:
279
279
280
280
```
@@ -286,12 +286,32 @@ fn foo<T>(x: T) {
286
286
}
287
287
```
288
288
289
- Functions inside functions are basically just like top-level functions, except
290
- that they can only be called from the function they are in.
289
+ nor will this:
290
+
291
+ ```
292
+ fn foo<T>(x: T) {
293
+ type MaybeT = Option<T>;
294
+ // ...
295
+ }
296
+ ```
297
+
298
+ or this:
299
+
300
+ ```
301
+ fn foo<T>(x: T) {
302
+ struct Foo {
303
+ x: T,
304
+ }
305
+ // ...
306
+ }
307
+ ```
308
+
309
+ Items inside functions are basically just like top-level items, except
310
+ that they can only be used from the function they are in.
291
311
292
312
There are a couple of solutions for this.
293
313
294
- You can use a closure:
314
+ If the item is a function, you may use a closure:
295
315
296
316
```
297
317
fn foo<T>(x: T) {
@@ -302,7 +322,7 @@ fn foo<T>(x: T) {
302
322
}
303
323
```
304
324
305
- or copy over the parameters:
325
+ For a generic item, you can copy over the parameters:
306
326
307
327
```
308
328
fn foo<T>(x: T) {
@@ -313,6 +333,12 @@ fn foo<T>(x: T) {
313
333
}
314
334
```
315
335
336
+ ```
337
+ fn foo<T>(x: T) {
338
+ type MaybeT<T> = Option<T>;
339
+ }
340
+ ```
341
+
316
342
Be sure to copy over any bounds as well:
317
343
318
344
```
@@ -324,10 +350,18 @@ fn foo<T: Copy>(x: T) {
324
350
}
325
351
```
326
352
353
+ ```
354
+ fn foo<T: Copy>(x: T) {
355
+ struct Foo<T: Copy> {
356
+ x: T,
357
+ }
358
+ }
359
+ ```
360
+
327
361
This may require additional type hints in the function body.
328
362
329
- In case the function is in an `impl`, defining a private helper function might
330
- be easier:
363
+ In case the item is a function inside an `impl`, defining a private helper
364
+ function might be easier:
331
365
332
366
```
333
367
impl<T> Foo<T> {
0 commit comments