此处的文档定义了一个特征,除其他外,该特征具有一个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;
我想您正在使用0.10文档和一个主版本。为了为勇敢的动态尺寸类型的新世界做准备,其中~str
(aka Box<str>
)不包含容量,而仅包含长度,因此由于每次都需要重新分配而无法有效地推入,因此已将这些方法从中删除OwnedStr
。现在,您应该StrBuf
改为处理类似的方法push_char
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句