相关疑难解决方法(0)

当提供的输入是字符串引用时,str::contains 不起作用

use std::collections::HashSet;

fn character_count(needles: &HashSet<char>, needle_type: &str, input: &str) -> i32 {
    for needle in needles {
        let mut needle_custom;

        if needle_type == "double" {
            needle_custom = needle.to_string() + &needle.to_string();
        } else {
            needle_custom = needle.to_string() + &needle.to_string() + &needle.to_string();
        }

        if input.contains(needle_custom) {
            println!("found needle {:?}", needle_custom);
        }
    }

    return 1;
}
Run Code Online (Sandbox Code Playgroud)
use std::collections::HashSet;

fn character_count(needles: &HashSet<char>, needle_type: &str, input: &str) -> i32 {
    for needle in needles {
        let mut needle_custom;

        if needle_type == "double" {
            needle_custom = …
Run Code Online (Sandbox Code Playgroud)

string rust

6
推荐指数
1
解决办法
1978
查看次数

在尝试拆分字符串时,没有为`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
查看次数

标签 统计

rust ×2

string ×1