Better way to code this snippet

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

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

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

    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 // Lib.rs?

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.