The trait `FnMut<(char,)>` is not implemented for `String` when trying to split a string

前端 未结 2 679
[愿得一人]
[愿得一人] 2020-11-29 12:59

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

use std::str::Split;

fn main() {
    let x = \"\".to_strin         


        
相关标签:
2条回答
  • 2020-11-29 13:11

    I talked about this with #rust-beginners IRC channel and heard the following:

    15:12:15           achird | d33tah: split accepts a Pattern, where Pattern can be &str or char, but you're passing a String (no idea why deref is not working)
    15:13:01           d33tah | achird: thanks! how can I convert string to str?
    15:13:03           achird | i think a simple split(&delimiter2) should fix the problem
    15:16:26           calops | why isn't deref working though?
    15:21:33        @mbrubeck | calops, d33tah: Deref coercions only work if one exact "expected" type is known.  For a generic type like <P: Pattern>, coercion doesn't kick in.
    15:24:11        @mbrubeck | d33tah: The error should definitely be improved...  It should complain that `String` doesn't impl `Pattern`, instead of jumping straight to `FnMut(char)`
    

    So basically, the solution is to add & before the delimiter string, like this:

    fn main() {
        let s1 = "".to_string();
        let s2 = "".to_string();
        let x = s1.split(&s2);
    }
    
    0 讨论(0)
  • 2020-11-29 13:18

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

    • A &str,
    • A char,
    • A closure,

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

    Example:

    fn main() {
        let x = "".to_string();
        let split = x.split("");
    }
    
    0 讨论(0)
提交回复
热议问题