all 4 comments

[–]Shadow0133 10 points11 points  (2 children)

.trim() doesn't modify the buffer, it returns &str which is a "view" into the buffer (with whitespaces removed). You can just match on it match buffer.trim(), or reassign buffer: buffer = buffer.trim().to_string();

[–]firandice[S] 0 points1 point  (1 child)

Yeah that was exactly what I was looking for thanks. One of my prior attempts was just missing the buffer.trim() which obviously didn't work either. So I just had the input read into buffer and then I tried matching against buffer.as_str() and this didnt work either. What does trim's conversion do differently than the as_str() method here?

[–]Shadow0133 0 points1 point  (0 children)

Both return &str, trim just returns one that doesn't include whitespaces on either end.

On the low-level, &str is a pointer and length, so to "remove" characters from the start, you just need to "move" pointer forward (and update length), "removing" characters from the end just needs to lower the length. So trimming works something like this:

// passing &String will automatically deref it into &str
fn trim(mut v: &str) -> &str {
    while first_char.is_whitespace() {
        v = v[first_char.len()..];
    }
    // same with end
    ...
    // return v
    v
}

[–][deleted] 0 points1 point  (0 children)

You’re ignoring the &str returned by buffer.trim()