【发布时间】:2014-05-21 22:29:46
【问题描述】:
文档here 定义了一个特征,其中包括一个方法push_char,它接受一个可变的self 并向其附加一个字符。但是,此代码失败:
fn foo() {
let mut s = "hey".to_owned();
s.push_char('!');
}
正在尝试编译:
$ rustc --version
rustc 0.11-pre (e8053b9 2014-05-12 09:12:04 -0700)
host: x86_64-apple-darwin
$ rustc appendchar.rs
appendchar.rs:5:5: 5:19 error: type `~str` does not implement any method in scope named `push_char`
appendchar.rs:5 s.push_char('!');
^~~~~~~~~~~~~~
error: aborting due to previous error
所以我们可以看到s 确实是~str 类型,并且根据文档,这种类型实现了OwnedStr。那么为什么会失败呢?顺便说一句,添加以下行并不能解决它:
use std::str::OwnedStr;
【问题讨论】: