How to get the word before the last word from a string (edge‑case‑safe) in Rust

1 Answer

0 votes
/**
 * Returns the word before the last word.
 * Uses Rust's native Unicode-aware char methods to handle international text.
 */
fn get_word_before_last(text: &str) -> String {
    // 1. Split the string by any character that is NOT alphanumeric.
    //    Rust's `is_alphanumeric` handles Unicode (Kanji, Cyrillic, etc.) natively.
    // 2. Filter out empty strings caused by multiple spaces or punctuation.
    // 3. Collect into a Vector of string slices.
    let words: Vec<&str> = text
        .split(|c: char| !c.is_alphanumeric())
        .filter(|s| !s.is_empty())
        .collect();

    // Check if we have at least two words
    if words.len() < 2 {
        return "null".to_string();
    }

    // Return the second to last word
    words[words.len() - 2].to_string()
}

fn main() {
    println!("=== Testing: Get Word Before Last ===\n");

    let tests = vec![
        "python rust",
        "  many   spaces   here   now  ",
        "OneWord",
        "",
        "   ",
        "Hello, world!",
        "Tabs\tand\nnewlines work too",
        "Unicode 世界、こんにちは",
        "Ends with punctuation.",
        "Multiple words, with punctuation, here!",
        "state-of-the-art program example",
    ];

    for t in tests {
        let result = get_word_before_last(t);
        
        println!("Input: \"{}\"", t);
        println!("Output: {}", result);
        println!("{}", "-".repeat(40));
    }
}


/*
OUTPUT:

=== Testing: Get Word Before Last ===

Input: "python rust"
Output: python
----------------------------------------
Input: "  many   spaces   here   now  "
Output: here
----------------------------------------
Input: "OneWord"
Output: null
----------------------------------------
Input: ""
Output: null
----------------------------------------
Input: "   "
Output: null
----------------------------------------
Input: "Hello, world!"
Output: Hello
----------------------------------------
Input: "Tabs	and
newlines work too"
Output: work
----------------------------------------
Input: "Unicode 世界、こんにちは"
Output: 世界
----------------------------------------
Input: "Ends with punctuation."
Output: with
----------------------------------------
Input: "Multiple words, with punctuation, here!"
Output: punctuation
----------------------------------------
Input: "state-of-the-art program example"
Output: program
----------------------------------------

*/

 



answered Mar 29 by avibootz

Related questions

...