简体   繁体   English

在Rust中,“让x = ~10;”过时了吗?

[英]Is “let x = ~10;” obsolete in Rust?

I read this tutorial and tried the following Rust code: 我阅读了本教程并尝试了以下Rust代码:

fn main() {
    let x = ~10;
    println!("{:d}", *x);
}

But the compiler complains: 但编译器抱怨:

rustc 1.16.0 (30cf806ef 2017-03-10)
error: expected expression, found `~`
 --> <anon>:2:13
  |
2 |     let x = ~10;
  |             ^

error: unknown format trait `d`
 --> <anon>:3:22
  |
3 |     println!("{:d}", *x);
  |                      ^^

Is let x = ~10; let x = ~10; obsolete already? 已经过时了?

It is very obsolete. 这是非常过时的。 Rust 1.0 was released on 2015-05-15. Rust 1.0于2015-05-15发布。 This syntax was removed months before that. 此语法在此之前几个月被删除。 That means that your tutorial hasn't been updated in a long time; 这意味着您的教程很长时间没有更新; in fact, that file was last updated on 2014-01-28! 事实上,该文件最后更新时间是2014-01-28! Not a good sign. 不是一个好兆头。

The non-obsolete version of your code: 您的代码的非过时版本:

fn main() {
    let x = Box::new(10);
    println!("{}", x);
}
  1. The sigil ~ was replaced with specific data structures. sigil ~被特定的数据结构取代。 In this case, Box . 在这种情况下, Box
  2. The format specifier :d no longer exists. 格式说明符:d不再存在。 Just use the Display formatter {} . 只需使用Display格式化程序{}
  3. There's no need to dereference the boxed number. 没有必要取消引用盒装号码。

Instead of some "already obsolete" reference, use the official sources: 使用官方消息来源,而不是一些“已经过时”的参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM