2.10.1 别名涉及时的签名修改建议

译文 · 基于 Learning Rust

别名涉及时的签名修改建议

原文链接: https://quinedot.github.io/rust-learning/c-signatures.html

这是本指南前面出现的一个场景。 编译器的建议是:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
error[E0621]: explicit lifetime required in the type of `s`
 --> src/lib.rs:5:9
  |
5 |         Self(s)
  |         ^^^^^^^ lifetime `'a` required
  |
help: add explicit lifetime `'a` to the type of `s`
  |
4 |     fn new(s: &'a str) -> Node<'_> {
  |                ++

但这样同样有效:

1
2
-        Self(s)
+        Node(s)

在实现 trait 时,你甚至可能收到上述编译器建议,而那时你通常不能修改签名。

最后修改 August 23, 2026: 更新 (499855b16)