Help to shorten the code

I have the following code

fn main() {
    let text = ".?;,!!!";
    println!("{:?}", norm(text));

    let text = ".?;,!!";
    println!("{:?}", norm(text));

    let text = ".?;,!";
    println!("{:?}", norm(text));
}

fn norm<'a>(splittersp: &'a str) -> (&'a str, bool) {
    let mut res = false;
    let splitters = if let Some(stripped) = splittersp.strip_suffix("!!") {
        if splittersp.len() > 2 {
            if splittersp.chars().nth_back(2).unwrap() != '!' {
                &splittersp[..splittersp.len() - 1]
            } else {
                res = true;
                stripped
            }
        } else {
            &splittersp[..splittersp.len() - 1]
        }
    } else if let Some(stripped) = splittersp.strip_suffix("!") {
        res = true;
        stripped
    } else {
        splittersp
    };
    (splitters, res)
}

Specifically, I do not like the if splittersp.chars().nth_back(2).unwrap() != '!' {. Is there any way to express it shorter without unwrap?

Use is_some_and.

splittersp.chars().nth_back(2).is_some_and(|chr| chr != '!')

Or, better, use if let to avoid double checks for the length:

        if let Some(chr) = splittersp.chars().nth_back(2) {
            if chr != '!' {
                &splittersp[..splittersp.len() - 1]
            } else {
                res = true;
                stripped
            }
        } else {
            &splittersp[..splittersp.len() - 1]
        }

Side note: Your code may panics at unwrap() if input string contains non-ASCII characters.

Unless I'm mistaken, it looks perhaps equivalent to the following. I think it might be a bit easier to work with if you keep things flat and serialized?

fn norm2(orig: &str) -> (&str, bool) {
    // Try to strip off one ! If we can't return.
    let Some(first) = orig.strip_suffix("!") else {
        return (orig, false);
    };

    // Try to strip off another !, if we can't return
    // what we had already stripped.
    let Some(second) = first.strip_suffix("!") else {
        return (first, true);
    };

    // Finally, check if there was a third !, if there was
    // not, then we return only the first split.
    if !second.ends_with("!") {
        return (first, false);
    }

    // Otherwise, accept our second split with true.
    (second, true)
}

I like the solution so far.