离大致看明白那段经典反面例程大概还有五六步。

如果把昨天的 struct 个泛<类>(类); 里的 <类> 去掉,报错如下:

error[E0412]: cannot find type `类` in this scope
  --> src/main.rs:12:11
   |
6  | struct 个(甲);
   | -------------- similarly named struct `个` defined here
...
12 | struct 个泛(类);
   |             ^^ help: a struct with a similar name exists: `个`

error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
  --> src/main.rs:21:13
   |
21 |     let _字: 个泛<char> = 个泛('a');
   |              ^^^^------ help: remove the unnecessary generics
   |              |
   |              expected 0 generic arguments
   |
note: struct defined here, with 0 generic parameters
  --> src/main.rs:12:8
   |
12 | struct 个泛(类);
   |        ^^^^

Some errors have detailed explanations: E0107, E0412.
For more information about an error, try `rustc --explain E0107`.

仔细看的话,error[E0107]error[E0412] 之间还是有隔一行,加上字体颜色加强了分隔效果。

如果加多一个泛型参数类,则报错:

error[E0392]: type parameter `类2` is never used
  --> src/main.rs:12:14
   |
12 | struct 个泛<类, 类2>(类);
   |                 ^^^ unused type parameter
   |
   = help: consider removing `类2`, referring to it in a field, or using a marker such as `PhantomData`
   = help: if you intended `类2` to be a const parameter, use `const 类2: /* Type */` instead

error[E0107]: struct takes 2 generic arguments but 1 generic argument was supplied
  --> src/main.rs:21:13
   |
21 |     let _字: 个泛<char> = 个泛('a');
   |              ^^^^ ---- supplied 1 generic argument
   |              |
   |              expected 2 generic arguments
   |
note: struct defined here, with 2 generic parameters: `类`, `类2`
  --> src/main.rs:12:8
   |
12 | struct 个泛<类, 类2>(类);
   |        ^^^^ --  ---
help: add missing generic argument
   |
21 |     let _字: 个泛<char, 类2> = 个泛('a');
   |                       +++++

Some errors have detailed explanations: E0107, E0392.
For more information about an error, try `rustc --explain E0107`.

想起之前的 unused variable 是个警告而非错误,类和变量在这方面需要区别对待吗?

继续泛型的 函数一节

reg_fn。。。挺反感这种缩写。大概是 regular?gen_spec_ 更是,就算估计是 generic_specified_,结合注释也只能猜个大概。

另外,又想到,昨天例程中,可以这样: let _类: 个泛<甲> = 个泛(甲); ,但这样的话:let _字: 个泛<'a'> = 个泛('a');就报错如下,有点不一致的感觉:

error[E0747]: constant provided when a type was expected
  --> src/main.rs:21:16
   |
21 |     let _字: 个泛<'a'> = 个泛('a');
   |                   ^^^

For more information about this error, try `rustc --explain E0747`.

中文化后例程,通过编译:

struct ;          // Concrete type `甲`.
struct ();       // Concrete type `构`.
struct 泛构<>(); // Generic type `泛构`.

// The following functions all take ownership of the variable passed into
// them and immediately go out of scope, freeing the variable.

// Define a function `普通函数` that takes an argument `_构` of type `构`.
// This has no `<类>` so this is not a 泛型 function.
fn 普通函数(_构: ) {}

// Define a function `指定类泛型` that takes an argument `_构` of type `泛构<类>`.
// It has been explicitly given the type parameter `甲`, but because `甲` has not 
// been specified as a 泛型 type parameter for `指定类泛型`, it is not 泛型.
fn 指定类泛型(_构: 泛构<>) {}

// Define a function `指定数泛型` that takes an argument `_构` of type `泛构<i32>`.
// It has been explicitly given the type parameter `i32`, which is a specific type.
// Because `i32` is not a 泛型 type, this function is also not 泛型.
fn 指定数泛型(_构: 泛构<i32>) {}

// Define a function `泛型` that takes an argument `_构` of type `泛构<类>`.
// Because `泛构<类>` is preceded by `<类>`, this function is 泛型 over `类`.
fn 泛型<>(_构: 泛构<>) {}

fn main() {
    // Using the non-泛型 functions
    普通函数(());          // Concrete type.
    指定类泛型(泛构());   // Implicitly specified type parameter `甲`.
    指定数泛型(泛构(6)); // Implicitly specified type parameter `i32`.

    // Explicitly specified type parameter `char` to `泛型()`.
    泛型::<char>(泛构('a'));

    // Implicitly specified type parameter `char` to `泛型()`.
    泛型(泛构('c'));
}

泛型::<char>(泛构('a')); 特别扎眼。为啥不像 struct 个泛<类>(类); 那样,非要加 :: 呢?即便引用 个泛 时,也是 let _字: 个泛<char> = 个泛('a'); 这样 <char> 之前并无 ::。去掉的话,报错如下:

error: comparison operators cannot be chained
  --> src/main.rs:33:7
   |
33 |     泛型<char>(泛构('a'));
   |         ^    ^
   |
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
   |
33 |     泛型::<char>(泛构('a'));
   |         ++

提示很明确,但不知为何语法上这么设计。

试了 泛型::<String>(泛构("a".to_string())); 也可以。

泛构 的参数,除了 struct 名之外,估计可以是内置类型(primitives)的值(如 ‘a’, 6),以及 泛型::<&str>(泛构("a"));