Skip to content

How to trim in Rust #2644

Answered by seanmonstar
sm8082 asked this question in Q&A
Discussion options

You must be logged in to vote

You could parse the bytes as a string with from_utf8, and then use str.trim():

match std::str::from_utf8(&body) {
    Ok(text) => {
        let count_it = text.trim().len().to_string();
        *response.body_mut() = count_it.into();
    },
    Err(_not_utf8) => {
        *response.status_mut() = StatusCode::BAD_REQUEST;
        *response.body_mut() = "Not UTF-8".into();
    }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by seanmonstar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2642 on September 13, 2021 18:32.