Converting &[&char] to String

Hey there,

I'm attempting to use Rust for some text processing, but am struggling to fully comprehend how I'd deal with the following.

I'm using the following:

sequence.chars().collect::,Vec<char>>().iter().collect::<Vec<_>>().windows(2);

To get all combinations of two characters, however I'm struggling to join the characters using the to_string() method.

  --> src/lib.rs:20:36
   |
20 |             println!("{:?}", slice.to_string());
   |                                    ^^^^^^^^^
   |
   = note: the method `to_string` exists but the following trait bounds were not satisfied:
           `&[&char] : std::string::ToString`
           `[&char] : std::string::ToString`

I don't even know what the type &[&char] means - given the fact that there is certainly 2 characters in the given window, so I'm struggling to get my head around it.

Best wishes,

Keiron

&[&char] is a slice of char references - Rust can't know that there are 2 at compile time, since the variable passed to windows could potentially be decided at runtime (this should be able to be changed with const generics.) You can collect it into a string by doing slice.iter().copied().collect::<String>().

4 Likes

Thank you so much for the help!

I'm astonished at the performance improvements Rust is giving me.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.