Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Convert Lines Of Characters Into A Vec Of Vecs In Rust

Code

fn main() {
  let cases = vec![
    (
      "abcd\nefgh",
      vec![vec!['a', 'b', 'c', 'd'], vec!['e', 'f', 'g', 'h']]
    )
  ];

  cases.iter().for_each(|case| {
    let left = split_line_chars(case.0);
    dbg!(&left);
    assert_eq!(left, case.1);
  });

}

fn split_line_chars(source: &str) -> Vec<Vec<char>> {
  let lines = source.lines();
  lines.map(|line| line.chars().map(|c| c).collect()).collect()
}

Results

[neopolitan_code_run:12] &left = [
    [
        'a',
        'b',
        'c',
        'd',
    ],
    [
        'e',
        'f',
        'g',
        'h',
    ],
]