Skip to content

Conversation

@Rudxain
Copy link
Contributor

@Rudxain Rudxain commented Mar 7, 2025

Technically, this change doesn't directly test for idempotence, as the fn is called once instead of being chained. I still believe it adds value, because it can make the challenge "harder" while still being fair.

Note

The following is just feedback from my recent experience. It can be ignored as "off-topic", but I believe it might be helpful for designing exercises in general.

BTW, when I was solving it, I felt like using trim method was "cheating", so I implemented it myself:

fn trim_me(input: &str) -> &str {
    let l = |c| c != ' ';
    let start = input.find(l).unwrap_or_default();
    // it's easier to let patterns do their job
    // than to handle arithmetic overflows manually,
    // hence the asymmetry
    match input.rfind(l) {
        Some(end) => &input[start..=end],
        _ => &input[start..],
    }
}

Originally, the code was this:

fn trim_me(input: &str) -> &str {
    let l = |c| c != ' ';
    &input[input.find(l).unwrap_or_default()..=input.rfind(l).unwrap_or(input.len() - 1)]
}

Aside from being an eyesore, it doesn't work for "" (input.len() - 1 is always an invalid index). That's why I added some extra tests locally, as a "challenge" to myself.

Should I add the empty str test?

@mo8it mo8it merged commit a99433c into rust-lang:main May 13, 2025
7 checks passed
@Rudxain Rudxain deleted the strim branch May 15, 2025 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants