编者注:在Rust 1.0发布之前询问了这个问题
..并引入了"范围"操作符.问题的代码不再代表当前的样式,但下面的一些答案使用的代码可以在Rust 1.0及以后版本中使用.
我在Rust by Example网站上玩,想要反过来打印出fizzbuzz.这是我尝试过的:
fn main() {
// `n` will take the values: 1, 2, ..., 100 in each iteration
for n in std::iter::range_step(100u, 0, -1) {
if n % 15 == 0 {
println!("fizzbuzz");
} else if n % 3 == 0 {
println!("fizz");
} else if n % 5 == 0 {
println!("buzz");
} else {
println!("{}", n);
}
}
}
Run Code Online (Sandbox Code Playgroud)
没有编译错误,但它没有打印出任何东西.如何从100迭代到1?
我正在尝试创建pub fn sing(start: i32, end: i32) -> String它返回一个串联的字符串,pub fn verse(num: i32) -> String重复调用每个数字之间start和end.
我用google搜索答案,看起来Rust String连接回答了我的问题,如果我甚至在操场上编写我的代码就行了,但是:
我的代码:
pub fn verse(num: i32) -> String {
match num {
0 => "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".to_string(),
1 => "1 bottle of beer on the wall, 1 bottle of beer.\nTake …Run Code Online (Sandbox Code Playgroud)