为什么Rust有String和str?String和之间有什么区别str?什么时候使用String而不是str反之亦然?其中一个被弃用了吗?
我需要拆分另一个String(不&str)String:
use std::str::Split;
fn main() {
let x = "".to_string().split("".to_string());
}
Run Code Online (Sandbox Code Playgroud)
为什么我会遇到此错误以及如果我必须对字符串进行操作,如何避免它?
error[E0277]: the trait bound `std::string::String: std::ops::FnMut<(char,)>` is not satisfied
--> src/main.rs:4:32
|
4 | let x = "".to_string().split("".to_string());
| ^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String`
|
= note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `std::string::String`
Run Code Online (Sandbox Code Playgroud)
根据# Derefstix- beginners IRC频道,这可能是1.20.0-夜间失败的一个例子.如何在Rust中拆分字符串?不按地址分割的问题String,不是&str.
let mystring = format!("the quick brown {}", "fox...");
assert!(mystring.ends_with(mystring));
Run Code Online (Sandbox Code Playgroud)
错误:
the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String`
Run Code Online (Sandbox Code Playgroud)
改变mystring.ends_with(mystring)以mystring.ends_with(mystring.as_str())修复它.
为什么这个错误如此神秘?
如果我在不使用格式的情况下创建字符串,请说:
let mystring = String::from_str("The quick brown fox...");
assert!(mystring.ends_with(mystring));
Run Code Online (Sandbox Code Playgroud)
错误更容易理解:
error[E0599]: no method named `ends_with` found for type
`std::result::Result<std::string::String, std::string::ParseError>`
in the current scope
Run Code Online (Sandbox Code Playgroud)