弦的反向单词

发布于 2025-02-10 19:35:04 字数 605 浏览 1 评论 0原文

处理字符串后,我正在尝试在单词之间显示空间,但是我得到了

thread 'main' panicked at 'assertion failed: `(left == right)`
left: `"abcd"`,
right: `"a b c d"`', src/main.rs:11:5

如何修复?

fn reverse_words(words: &str) -> String {
    words.split_whitespace().map(reverse_word).collect()
}

fn reverse_word(word: &str) -> String {
    word.chars().rev().collect();
}

fn main() {
    assert_eq!(reverse_words("apple"), "elppa");
    assert_eq!(reverse_words("a b c d"), "a b c d");
    assert_eq!(reverse_words("double  spaced  words"), "elbuod  decaps  sdrow");
}

I'm trying to show spaces between the words after processing the string but I'm getting

thread 'main' panicked at 'assertion failed: `(left == right)`
left: `"abcd"`,
right: `"a b c d"`', src/main.rs:11:5

How can I fix it?

fn reverse_words(words: &str) -> String {
    words.split_whitespace().map(reverse_word).collect()
}

fn reverse_word(word: &str) -> String {
    word.chars().rev().collect();
}

fn main() {
    assert_eq!(reverse_words("apple"), "elppa");
    assert_eq!(reverse_words("a b c d"), "a b c d");
    assert_eq!(reverse_words("double  spaced  words"), "elbuod  decaps  sdrow");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫瑟鸿黎 2025-02-17 19:35:04

您将输入分配在Whitespace上,但是当您收集它时,它会在没有任何分离器的情况下加入。要修复,您可以将其收集到vec< string>上,然后在其上调用.join(“”)

fn reverse_words(words: &str) -> String {
    words.split_whitespace().map(reverse_word).collect::<Vec<_>>().join(" ")
}


;不是分配中间的VEC:

fn reverse_words(words: &str) -> String {
    words
        .split_whitespace()
        .map(reverse_word)
        .enumerate()
        .fold(
            String::new(),
            |acc, (i, word)| if i == 0 { word } else { acc + " " + &word },
        )
}

You split the input on whitespace but when you collect it, it's joined without any separator. To fix, you can collect it into a Vec<String> and then call .join(" ") on it:

fn reverse_words(words: &str) -> String {
    words.split_whitespace().map(reverse_word).collect::<Vec<_>>().join(" ")
}

Playground


A more efficient implementation of reverse_words that does not allocate an intermediate Vec:

fn reverse_words(words: &str) -> String {
    words
        .split_whitespace()
        .map(reverse_word)
        .enumerate()
        .fold(
            String::new(),
            |acc, (i, word)| if i == 0 { word } else { acc + " " + &word },
        )
}

Playground

我早已燃尽 2025-02-17 19:35:04
fn reverse_words(words: &str) -> String {
    words.to_string()
        .split(" ")
        .map(reverse_string)
        .collect::<Vec<String>>()
        .join(" ")
}

fn reverse_string(s: &str) -> String {
    s.to_string()
        .chars()
        .rev()
        .collect::<String>()
}
fn reverse_words(words: &str) -> String {
    words.to_string()
        .split(" ")
        .map(reverse_string)
        .collect::<Vec<String>>()
        .join(" ")
}

fn reverse_string(s: &str) -> String {
    s.to_string()
        .chars()
        .rev()
        .collect::<String>()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文