我..=
在一些Rust代码中看到了这个运算符:
for s in 2..=9 {
// some code here
}
Run Code Online (Sandbox Code Playgroud)
它是什么?
我想创建一个带有'a'..'z'值(包括)的向量.
这不编译:
let vec: Vec<char> = ('a'..'z'+1).collect();
Run Code Online (Sandbox Code Playgroud)
什么是惯用的方式'a'..'z'
?
我正在尝试理解...
语法.考虑以下程序:
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();
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,9...11
模式匹配中使用的编译很好.当我尝试使用相同的内容时,println!("{:?}", (2...6));
我得到编译错误:
error: `...` syntax cannot be used in expressions …
Run Code Online (Sandbox Code Playgroud)