为什么不能在表达式中使用 ... 语法? [英] Why it is not possible to use the ... syntax in expressions?

查看:43
本文介绍了为什么不能在表达式中使用 ... 语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解 ... 语法.考虑以下程序:

fn how_many(x: i32) ->&'static str {匹配 x {0 =>没有橘子",1 =>一个橙子",2 |3 =>两三个橙子",9...11 =>大约 10 个橙子",_ =>几个橙子",}}fn 模式匹配(){对于 x 在 0..13 {println!("{} : 我有 {} ", x, how_many(x));}}fn 主(){//println!("{:?}", (2...6));模式匹配();}

在上面的程序中,模式匹配中使用的 9...11 编译得很好.当我尝试在 println!("{:?}", (2...6)); 中使用相同的代码时,出现编译错误:

error: `...` 语法不能在表达式中使用-->src/main.rs:18:24|18 |println!("{:?}", (2...6));|^^^|= help: 如果您需要独占范围 (a < b),请使用 `..`= help: 或者 `..=` 如果你需要一个包含范围 (a <= b)

我试图理解为什么不能在 println 中使用.

解决方案

与不能在表达式中使用 &*$# 的原因相同:Rust 的语法不允许这样做.Rust 决定对包含范围使用不同的语法.如果你真的关心完整的细节,你可以阅读关于该问题的所有 350 多条评论.

TL;DR:

  • 没有人喜欢特定的语法
  • ... 太接近 ..
  • 的拼写错误模式中的
  • ... 已经稳定,必须永远支持,但它会在某些时候被弃用,..= 也将成为首选.

错误消息告诉您要使用的适当语法:

<块引用>

 = help: 如果需要独占范围,请使用 `..` (a < b)= help: 或者 `..=` 如果你需要一个包含范围 (a <= b)

fn how_many(x: i32) ->&'static str {匹配 x {0 =>没有橘子",1 =>一个橙子",2 |3 =>两三个橙子",9..=11 =>大约 10 个橙子",_ =>几个橙子",}}fn 模式匹配(){对于 x 在 0..13 {println!("{} : 我有 {} ", x, how_many(x));}}fn 主(){println!("{:?}", 2..=6);模式匹配();}

I'm trying understand the ... syntax. Consider the following program:

fn how_many(x: i32) -> &'static str {
    match x {
        0 => "no oranges",
        1 => "an orange",
        2 | 3 => "two or three oranges",
        9...11 => "approximately 10 oranges",
        _ => "few oranges",
    }
}

fn pattern_matching() {
    for x in 0..13 {
        println!("{} : I have {} ", x, how_many(x));
    }
}

fn main() {
    // println!("{:?}", (2...6));
    pattern_matching();
}

In the above program, the 9...11 used in pattern matching compiles fine. When I try to use the same in like println!("{:?}", (2...6)); I get compilation error:

error: `...` syntax cannot be used in expressions
  --> src/main.rs:18:24
   |
18 |     println!("{:?}", (2...6));
   |                        ^^^
   |
   = help: Use `..` if you need an exclusive range (a < b)
   = help: or `..=` if you need an inclusive range (a <= b)

I'm trying to understand why it is not possible to use within println.

解决方案

For the same reason that you cannot use &*$# in expressions: it's not allowed by Rust's grammar. Rust decided to use different syntax for inclusive ranges. If you really care about the complete details, you can go read all 350+ comments on that issue.

The TL;DR:

  • No particular syntax was liked by everyone
  • ... is too close to a typo of ..
  • ... in patterns is already stabilized and must be supported forever, but it will be deprecated at some point and ..= will be preferred for that as well.

The error message tells you the appropriate syntax to use instead:

   = help: Use `..` if you need an exclusive range (a < b)
   = help: or `..=` if you need an inclusive range (a <= b)

fn how_many(x: i32) -> &'static str {
    match x {
        0 => "no oranges",
        1 => "an orange",
        2 | 3 => "two or three oranges",
        9..=11 => "approximately 10 oranges",
        _ => "few oranges",
    }
}

fn pattern_matching() {
    for x in 0..13 {
        println!("{} : I have {} ", x, how_many(x));
    }
}

fn main() {
    println!("{:?}", 2..=6);
    pattern_matching();
}

这篇关于为什么不能在表达式中使用 ... 语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆