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

d33*_*tah 5 rust

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

Fre*_*ios 6

一切都在文档中.您可以提供以下其中一项:

  • &str,
  • char,
  • 关闭,

这三种类型实现了Pattern特征.您都给人一种Stringsplit代替&str.

例:

fn main() {
    let x = "".to_string();
    let split = x.split("");
}
Run Code Online (Sandbox Code Playgroud)