Better way to code this snippet

What is the better & short way to code this snippet?

let html = r#"
        <!DOCTYPE html>
        <html lang="en">
                <meta charset="UTF-8" />
                <!-- This is Comment -->
                <a href="/nofollow" ref="nofollow">No Follow</a>
                <a href="/follow" ref="follow">Follow</a>

  let html: String = html
        .filter(|c| *c != '\n')
        .filter(|c| *c != '\t')
    let html: String = html
        .split(' ')
        .map(|c| c.trim())
        .filter(|c| c.len() > 0)
        .map(|c| format!("{0} ", c))

    println!("{:?}", html);

Please let me know.
Thank you

How about

let html = html.split_whitespace().collect::<Vec<_>>().join(" ");
1 Like

I tried to do it with as few iterations as possible, but it's not a general-purpose formatter.

1 Like

Why not use a Regex — Rust text processing library //

1 Like

I did it with regex & its very convenient.
But I am actually trying to understand the root level of rust without regex.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.