编辑代码

use std::io;

fn main() {
    println!("输入:");
    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();

    let abbreviation = acronym(&input);
    println!("首字母缩写:{}", abbreviation);
}

fn acronym(input: &str) -> String {
    let cleaned_name = remove_punctuation(input);
    let words: Vec<&str> = cleaned_name.split(|c| c == '-' || c == ' ').collect();

    let mut abbreviation = String::new();
    for word in words {
        if let Some(first_char) = first_letter(word) {
            abbreviation.push_str(&first_char.to_string());
        }
    }

    abbreviation
}

fn remove_punctuation(text: &str) -> String {
    let mut cleaned_text = String::new();
    for c in text.chars() {
        if c.is_alphabetic() || c == '-' || c == ' ' {
            cleaned_text.push(c);
        }
    }
    cleaned_text
}

fn first_letter(word: &str) -> Option<char> {
    word.chars().find(|c| c.is_alphabetic())
}