以下代码展示了问题的起源。由于
但是,在这种情况下使用时,需要添加类型注释。
as_slice()
已经被弃用,因此建议使用 as_ref()
进行替换。但是,在这种情况下使用时,需要添加类型注释。
let s = "Hi"; // This is a string slice
// warning: use of deprecated item: use std::convert::AsRef<str> instead, #[warn(deprecated)] on by default
assert!(s.replace("Hi", "Ho").as_slice() == "Ho");
// tests/lang.rs:120:35: 120:43 error: type annotations required: cannot resolve `collections::string::String : core::convert::AsRef<_>` [E0283]
// assert!(s.replace("Hi", "Ho").as_ref() == "Ho");
如何提供这样的类型注释?我唯一能找到有些适用的语法是<MyType as AsRef>::as_ref()
,但我不知道如何在实例中使用它。
我正在使用rustc 1.0.0-nightly (be9bd7c93 2015-04-05) (built 2015-04-05)
。