尝试拆分字符串时,未为 `String` 实现特性 `FnMut<(char,)>` [英] The trait `FnMut&lt;(char,)&gt;` is not implemented for `String` when trying to split a string

查看:29
本文介绍了尝试拆分字符串时,未为 `String` 实现特性 `FnMut<(char,)>`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个 String(不是 &str)拆分为另一个 String:

I need to split a String (not &str) by another String:

use std::str::Split;

fn main() {
    let x = "".to_string().split("".to_string());
}

如果我已经必须对字符串进行操作,为什么会出现此错误以及如何避免它?

Why do I get this error and how to avoid it if I already have to operate on strings?

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`

根据#rust-beginners IRC 频道,这可能是 Deref 在 1.20.0-nightly 中失败的一个例子.如何在 Rust 中拆分字符串? 没有解决问题按 String 而不是 &str 拆分.

According to the #rust-beginners IRC channel, this might be an example of Deref failing in 1.20.0-nightly. How to split a string in Rust? doesn't address the problem of splitting by String, not &str.

推荐答案

一切都在 文档.您可以提供以下之一:

All is in the documentation. You can provide one of:

  • A &str,
  • 一个 char,
  • 结束,

这三种类型实现了Pattern 特性.您给 split 一个 String 而不是 &str.

Those three types implement the Pattern trait. You are giving a String to split instead of a &str.

示例:

fn main() {
    let x = "".to_string();
    let split = x.split("");
}

这篇关于尝试拆分字符串时,未为 `String` 实现特性 `FnMut<(char,)>`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆