相关疑难解决方法(0)

Rust的`String`和`str`之间有什么区别?

为什么Rust有StringstrString和之间有什么区别str?什么时候使用String而不是str反之亦然?其中一个被弃用了吗?

string rust

341
推荐指数
13
解决办法
4万
查看次数

在尝试拆分字符串时,没有为`String`实现特性`FnMut <(char,)>`

我需要拆分另一个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.

rust

5
推荐指数
1
解决办法
969
查看次数

获取错误"特征`std :: ops :: FnMut <(char,)>`没有为`std :: string :: String`"实现简单类型不匹配

    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)

rust

2
推荐指数
1
解决办法
1545
查看次数

标签 统计

rust ×3

string ×1