How to write a nested array where the inner arrays are different lengths?

user5494880

I have a function that generates anagrams and I want to test it so I have the following:

pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
    unimplemented!()
}

fn main() {
    let words = [
        "eat".to_string(),
        "tea".to_string(),
        "tan".to_string(),
        "ate".to_string(),
        "nat".to_string(),
        "bat".to_string(),
    ]
    .to_vec();
    let answer = [["ate", "eat", "tea"], ["nat", "tan"], ["bat"]];
    let solution = group_anagrams(words);
    assert_eq!(answer, solution);
}

I can't figure out how to write this in a way that will compile:

error[E0308]: mismatched types
  --> src/main.rs:15:42
   |
15 |     let answer = [["ate", "eat", "tea"], ["nat", "tan"], ["bat"]];
   |                                          ^^^^^^^^^^^^^^ expected an array with a fixed size of 3 elements, found one with 2 elements
   |
   = note: expected type `[&str; 3]`
             found array `[&str; 2]`

(Playground)

John Kugelman

group_anagrams returns a Vec<Vec<String>>. One fix is to make answer nested Vecs instead of nested arrays to match.

let answer = vec![
    vec!["ate", "eat", "tea"],
    vec!["nat", "tan"],
    vec!["bat"],
];

If you didn't want nested Vecs you could try to use slices instead, since slices also move the compile-time lengths out of the types and into runtime storage.

let answer = [
    &["ate", "eat", "tea"][..],
    &["nat", "tan"][..],
    &["bat"][..],
];

Of course, this looks a lot uglier. And it still doesn't get the rest of the program to compile. (I'll leave that as an exercise for the reader.)


The original code fails to compile because the inner arrays have different lengths, and therefore different types: [&str; 3], [&str; 2], and [&str; 1], respectively. These are three distinct types and arrays can't contain heterogeneous items.

The original code would compile if the inner arrays happened to have the same length. If every array had three words then the overall type would be [[&str; 3]; 3]:

let answer: [[&str; 3]; 3] = [
    ["ate", "eat", "tea"],
    ["nat", "tan", "x"],
    ["bat", "y", "z"],
];

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Array of arrays in C, where the arrays are of different length

分類Dev

PHP Aligning multiple arrays of different lengths

分類Dev

How to write filter that inspects an array of arrays

分類Dev

Need to form an array object with nested data out of 3 different arrays

分類Dev

How to get inner array of nested object using underscorejs?

分類Dev

Merge three multidimensional array with different lengths

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

Different CHARACTER lengths (3/4) in array constructor, how to trim strings - fortran

分類Dev

How to work with the lists that have different lengths?

分類Dev

Ruby array of arrays find by inner array value

分類Dev

Javascript sum inner arrays in 3d nested arrays

分類Dev

Julia truncate inner dimension of array of arrays

分類Dev

How do I split an array into two different arrays?

分類Dev

How to write nested if in makefile?

分類Dev

Is index used for 'outer' and 'inner' where clauses in nested selects?

分類Dev

Merging the elements of nested arrays into one big array

分類Dev

How to generate an object with nested keys based on an array of arrays that hold it's key locations?

分類Dev

How to compare an array to an array of arrays?

分類Dev

How to insert an array into an array of arrays?

分類Dev

lodash where function and getting nested array elements

分類Dev

Array operations on two different arrays in ruby

分類Dev

Nested array in JSON to different rows in CSV

分類Dev

Asp.net : How to write a regular expression that validates strings of two lengths?

分類Dev

How to parse nested Json Arrays with Circe Optics

分類Dev

How to read MongoDB nested arrays into local memory

分類Dev

MongoDB - How to search a value into nested arrays?

分類Dev

How to write a stylesheet for Xml nested node with attributes?

分類Dev

Kotlin: How to write nested loops in functional way?

分類Dev

Trying to write a query that counts multiple things with different where cases

Related 関連記事

  1. 1

    Array of arrays in C, where the arrays are of different length

  2. 2

    PHP Aligning multiple arrays of different lengths

  3. 3

    How to write filter that inspects an array of arrays

  4. 4

    Need to form an array object with nested data out of 3 different arrays

  5. 5

    How to get inner array of nested object using underscorejs?

  6. 6

    Merge three multidimensional array with different lengths

  7. 7

    How to get the combination of array values from nested arrays in an array of objects

  8. 8

    Different CHARACTER lengths (3/4) in array constructor, how to trim strings - fortran

  9. 9

    How to work with the lists that have different lengths?

  10. 10

    Ruby array of arrays find by inner array value

  11. 11

    Javascript sum inner arrays in 3d nested arrays

  12. 12

    Julia truncate inner dimension of array of arrays

  13. 13

    How do I split an array into two different arrays?

  14. 14

    How to write nested if in makefile?

  15. 15

    Is index used for 'outer' and 'inner' where clauses in nested selects?

  16. 16

    Merging the elements of nested arrays into one big array

  17. 17

    How to generate an object with nested keys based on an array of arrays that hold it's key locations?

  18. 18

    How to compare an array to an array of arrays?

  19. 19

    How to insert an array into an array of arrays?

  20. 20

    lodash where function and getting nested array elements

  21. 21

    Array operations on two different arrays in ruby

  22. 22

    Nested array in JSON to different rows in CSV

  23. 23

    Asp.net : How to write a regular expression that validates strings of two lengths?

  24. 24

    How to parse nested Json Arrays with Circe Optics

  25. 25

    How to read MongoDB nested arrays into local memory

  26. 26

    MongoDB - How to search a value into nested arrays?

  27. 27

    How to write a stylesheet for Xml nested node with attributes?

  28. 28

    Kotlin: How to write nested loops in functional way?

  29. 29

    Trying to write a query that counts multiple things with different where cases

ホットタグ

アーカイブ