Rust程序打印的值与预期不同

edi*_*ion -9 rust

我正在学习Rust编程语言,并编写了一个包含简单for循环的测试程序:

fn test_func(max_rows: u32, max_cols: u32) {
    for y in 0..max_rows {
        for x in 0..max_cols {
            print!("{},{}", y, x);
        }
        print!("\n");
    }
}

fn main() {
    test_func(4, 4);
}
Run Code Online (Sandbox Code Playgroud)

产生以下内容:

0,00,10,20,3
1,01,11,21,3
2,02,12,22,3
3,03,13,23,3
Run Code Online (Sandbox Code Playgroud)

这与我用C语言编写的输出有所不同:

00,01,02,03
10,11,12,13
20,21,22,23
30,31,32,33
Run Code Online (Sandbox Code Playgroud)

为什么省略'x'值,为什么订单与我的预期不同?

oli*_*obk 6

您刚刚将逗号放在错误的位置.由于您没有提供等效的C代码,我们只能猜测,但您可能想要编写格式字符串

"{}{},"
Run Code Online (Sandbox Code Playgroud)