Start learning free

LESSON 615 OF 679 · PROJECTS: COMMAND-LINE TOOLS

CLI Tool: wc v1 — Count Lines

Practice

Build a tiny version of the Unix wc command. Given a list of lines, return the total number of lines as an integer.

Function signature:

fn count_lines(lines: Vec<String>) -> i32

Example:

Input: lines = ["hello", "world", "foo", "bar", "baz"]

Output: 5

Every entry of the list is one line, even an empty string "". An empty list has 0 lines.


The number of lines is just the vector's length. Use lines.len() and cast it to i32.

A one-liner:
impl Solution {
    fn count_lines(lines: Vec<String>) -> i32 {
        return lines.len() as i32;
    }
}

STEP 1 OF 1
    Ctrl + Enter to check