# Help to shorten the code

**URL:** <https://users.rust-lang.org/t/help-to-shorten-the-code/143728>\
**Category:** code review\
**Created:** [November 17, 2026, 4:38am UTC](https://users.rust-lang.org/t/help-to-shorten-the-code/143728 "2026-11-17T04:38:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MOCKBA](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mockba/32/41423_2.png) [@MOCKBA](https://users.rust-lang.org/u/MOCKBA)\
**Post date:** [November 17, 2026, 4:38am UTC](https://users.rust-lang.org/t/help-to-shorten-the-code/143728/1 "2026-11-17T04:38:40Z")

</div>

I have the following code

```rust
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`?

---

<div class="post-metadata">

**Author:** ![Schard](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/schard/32/43802_2.png) [@Schard](https://users.rust-lang.org/u/Schard)\
**Post date:** [November 17, 2026, 7:18am UTC](https://users.rust-lang.org/t/help-to-shorten-the-code/143728/2 "2026-11-17T07:18:24Z")

</div>

Use `is_some_and`.

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![zirconium-n](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zirconium-n/32/23899_2.png) [@zirconium-n](https://users.rust-lang.org/u/zirconium-n)\
**Post date:** [November 17, 2026, 7:22am UTC](https://users.rust-lang.org/t/help-to-shorten-the-code/143728/3 "2026-11-17T07:22:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sigseis](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sigseis/32/54746_2.png) [@sigseis](https://users.rust-lang.org/u/sigseis)\
**Post date:** [November 17, 2026, 7:25am UTC](https://users.rust-lang.org/t/help-to-shorten-the-code/143728/4 "2026-11-17T07:25:54Z")

</div>

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?

```rust
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)
}

```

---

<div class="post-metadata">

**Author:** ![MOCKBA](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mockba/32/41423_2.png) [@MOCKBA](https://users.rust-lang.org/u/MOCKBA)\
**Post date:** [November 17, 2026, 7:54pm UTC](https://users.rust-lang.org/t/help-to-shorten-the-code/143728/5 "2026-11-17T19:54:33Z")

</div>

I like the solution so far.
