相关疑难解决方法(0)

如何在Rust中拆分字符串?

文档来看,目前尚不清楚.在Java中,您可以使用如下split方法:

"some string 123 ffd".split("123");
Run Code Online (Sandbox Code Playgroud)

rust

103
推荐指数
6
解决办法
7万
查看次数

当提供的输入是字符串引用时,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
查看次数

获取错误"特征`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