相关疑难解决方法(0)

如何在Rust中对循环进行反向排序?

编者注:在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?

for-loop rust

39
推荐指数
2
解决办法
3万
查看次数

如何迭代向后范围?

我正在尝试创建pub fn sing(start: i32, end: i32) -> String它返回一个串联的字符串,pub fn verse(num: i32) -> String重复调用每个数字之间startend.

我用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)

iterator match rust

0
推荐指数
1
解决办法
212
查看次数

标签 统计

rust ×2

for-loop ×1

iterator ×1

match ×1