弃用属性

Minimum Rust version: 1.9

如果您正在编写库,并且想要弃用某些内容,则可以使用 deprecated 属性:

1. #[deprecated(
2. since = "0.2.1",
3. note = "Please use the bar function instead"
4. )]
5. pub fn foo() {
6. // ...
7. }

如果用户使用已弃用的功能,则会向您的用户发出警告:

1. Compiling playground v0.0.1 (file:///playground)
2. warning: use of deprecated item 'foo': Please use the bar function instead
3. --> src/main.rs:10:5
4. |
5. 10 |     foo();
6. |     ^^^
7. |
8. = note: #[warn(deprecated)] on by default

since 和 note 都是可选的。

since 可以是将来的; 你可以在那放任何东西,因为那儿并没有检查。