Using a type which doesn't implement some trait

I have a Vec<String> called a that contains ["aca", "aba", "aba", "cab", "bac"] but I want ["ac", "ca", "ab", "ba"...] I'm thinking to loop through the elements and separate them manually like this:

  let a: Vec<String> = (0..n).map(|_|;
  let p: Vec<String>;
  for i in 0..n {

Since I know that &a[0..2] and &a[1..3] contain the sub strings I want however I get an error which as a beginner I'm struggling to apply to the task at hand rustc --explain E0277. Any suggestions are much appreciated! Thanks.

Without seeing an example that has enough information, all I can say by looking at the code is that p would have to be mut.

Referencing an error number without saying what the error is makes it difficult for anyone to help you, as the example you provided doesn't have enough info to reproduce anything.

The problem is that the String cannot be indexed. If you do know that the strings in question are ASCII, you can use String::from_utf8(a[i].as_bytes()[0..2]).unwrap(). If there can be arbitrary Unicode, the best you can possibly do is to use something like chars().skip(prefix_len).take(substring_len).collect().

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