rust - 如何在match语句分支中声明变量?

标签 rust

我正在尝试解决一个任务
https://www.codewars.com/kata/591588d49f4056e13f000001/train/rust

Your task is to implement an simple interpreter for the notorious esoteric language HQ9+ that will work for a single character input:

  • If the input is 'H', return 'Hello World!'
  • If the input is 'Q', return the input
  • If the input is '9', return the full lyrics of 99 Bottles of Beer.

由于某种原因,我无法使其正常运行。我不知道我在这里错过了什么。如何在match语句分支中声明变量?
fn hq9(code: &str) -> Option<String> {
    match code{
        "H" => Some("Hello World!".to_string()),
        "Q" => Some("Q".to_string()),
        "9" => let s = String::new();
        (0..=99).rev().for_each(|x|
                                match x {
                                    x @ 3..=99 => s.push_str("{} bottles of beer on the wall, {} bottles of beer.\nTake one down and pass it around, {} bottles of beer on the wall.\n",x,x,x-1),
                                    2 => s.push_str("2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n"),
                                    1 => s.push_str("1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n"),
                                    0 => s.push_str("No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n"),
                                    _ => panic!(),
                                })
            Some(s),
        _  => None,
    }
}

最佳答案

匹配臂使用语法PATTERN => EXPRESSION,如果表达式需要多个语句,请使用block {}:

fn hq9(code: &str) -> Option<String> {
    match code {
        "H" => Some("Hello World!".to_string()),
        "Q" => Some("Q".to_string()),
        "9" => { // <----------------- start block
            let s = String::new();
            (0..=99).rev().for_each(|x|
                match x {
                    x @ 3..=99 => s.push_str("{} bottles of beer on the wall, {} bottles of beer.\nTake one down and pass it around, {} bottles of beer on the wall.\n",x,x,x-1),
                    2 => s.push_str("2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n"),
                    1 => s.push_str("1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n"),
                    0 => s.push_str("No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n"),
                    _ => panic!(),
                });
            Some(s)
        } // <----------------- end block
        _  => None,
    }
}
解决此问题会暴露其他错误。

关于rust - 如何在match语句分支中声明变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66608888/

相关文章:

rust - 当我重新分配链表的一半时,不再引用的 block 会发生什么?

rust - 为什么不能在同一结构中存储值和对该值的引用?

rust - 如何使用参数运行 PathBuf::new

rust - 循环的自动向量化,对 4 个 int 元素进行打乱,并取与之前排列的绝对差值

tcp - IRC 服务器不响应 Rust IRC 客户端识别请求

intellij-idea - 如何在 Intellij IDEA 中调试 Rust?

rust - 在Rust中挣扎着悬而未决的引用和静态生命周期建议

rust - 是否可以使用Iterator::filter_map添加条件过滤?

rust - 有没有一种惯用的方法来实现组件模式?

lambda - 在结构中存储 lambda 返回迭代器