typestar

Reverse a string in Rust

Reverses by chars, because a Rust String is UTF-8 and bytes are not characters.

fn reverse(s: &str) -> String {
    s.chars().rev().collect()
}

fn is_palindrome(s: &str) -> bool {
    let cleaned: String = s.chars().filter(|c| c.is_alphanumeric()).collect();
    cleaned.eq_ignore_ascii_case(&reverse(&cleaned))
}

Keywords and builtins used here

The run, in numbers

Lines
8
Characters to type
222
Tokens
75
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 47 seconds.

Type this snippet

Step 2 of 4 in Slices & strings, step 8 of 15 in Ownership & borrowing.

← Previous Next →

Reverse a string in other languages