I copied the reverse-string solution into a scratch crate and handed it an empty vector. It panicked before the loop ran once.

pub fn reverse_string(s: &mut Vec<char>) {
    let mut left = 0;
    let mut right = s.len() - 1;
    while left < right {
        s.swap(left, right);
        left += 1;
        right -= 1;
    }
}

s.len() is a usize, and usize has no negative value. On an empty vector, s.len() - 1 is a subtraction that goes below zero, and Rust does not let that happen quietly. Here is what it said:

thread 'main' panicked at src/main.rs:43:25:
attempt to subtract with overflow

That is the debug build. The release build does not panic there, because release builds have overflow checks turned off, so the subtraction wraps instead:

thread 'main' panicked at src/main.rs:45:15:
index out of bounds: the len is 0 but the index is 0

Still a panic, but a different one, one line later and for a different reason. s.len() - 1 wrapped to usize::MAX, 0 < usize::MAX was true, and s.swap(0, usize::MAX) walked off the front of a zero-length vector. The index named in the message is 0 and not 18446744073709551615, so the bounds check failed on the first index before the second one was considered.

The bug is real. The spec is not broken by it. The problem constrains 1 <= s.len() <= 10^5, so an empty vector is outside the contract, and the shipped code does not violate its own spec. Nothing is wrong here in the strict sense. A caller that hands this function an empty vector is breaking a rule that exists in a Markdown file, and the function’s response to that rule being broken is a panic on the third line rather than an empty result.

Two decisions in the signature

pub fn reverse_string(s: &mut Vec<char>) carries two decisions.

The &mut is there because the function mutates the caller’s buffer in place. The problem asks for O(1) extra memory, and &mut Vec<char> means the caller hands over a mutable borrow of the buffer it already owns. No copy, no clone, no second vector. The function returns nothing at all, because the answer is the caller’s own buffer, reversed in place.

Vec<char> rather than Vec<u8> is the other decision, and it is the one that keeps the function correct for input that is not plain ASCII. A UTF-8 string’s byte length is not its character length:

"héllo" bytes=6 chars=5
"日本語" bytes=9 chars=3

Reversing a Vec<u8> that holds “héllo” would swap the first byte with the last, and the last byte is the second half of a two-byte character. I did it: the bytes come back as 6f 6c 6c a9 c3 68, and str::from_utf8 rejects them. Vec<char> stores one char per element, one char per Unicode scalar value, so s.len() is the number of characters and every swap moves a whole character.

saturating_sub instead

This is my version; the repository still ships the subtraction.

pub fn reverse_string(s: &mut Vec<char>) {
    let mut left = 0;
    let mut right = s.len().saturating_sub(1);
    while left < right {
        s.swap(left, right);
        left += 1;
        right -= 1;
    }
}

saturating_sub clamps at zero instead of wrapping, so an empty vector gives right = 0, the loop condition left < right is false, and the function returns having done nothing. The subtraction no longer depends on a precondition the caller is supposed to have read about.

The rest of the loop stays safe on its own. The loop only runs while left < right, which means right is at least 1 when right -= 1 executes, so that subtraction cannot underflow either. It was only the initialization that was exposed.

Why swap and not two borrows

s.swap(left, right) is doing real work here. The obvious alternative is std::mem::swap(&mut s[left], &mut s[right]), which the borrow checker rejects, because it needs two mutable borrows of s at the same time and s is one vector. Vec::swap exists for exactly this case: it takes two indices, checks them against the length, and swaps in place without the caller ever holding two overlapping borrows.

The contract nobody enforces

The panic is the correct behavior for a function whose input contract was violated, and Rust’s debug build is the best witness to it: the failure lands on the line that made the bad assumption, with a message that names the assumption.

The contract still is not enforced by anything except the reader’s memory. s.len() - 1 is a place where the code states “there is at least one element” and the type system hears it as “there might be zero.” The subtraction is where the promise lives, and it is the only place the promise could have been checked.

The rule I take from this one: any arithmetic on a length goes through a saturating or checked variant, because the length is the one value in the function I do not control. The fix is one call.


Next: One Lookup, Not Two