gpt4 book ai didi

rust - 你如何在 Rust 中使用复数方法?

转载 作者:行者123 更新时间:2023-11-29 08:06:57 24 4
gpt4 key购买 nike

我一直对 Rust 的 std::fmt 模块中的 plural 功能感到困惑,但我需要一个具体的例子来实现它。

它的 0.9 文档位于页面的一半位置:http://static.rust-lang.org/doc/0.9/std/fmt/index.html

这对单词复数有用吗?例如,我将如何使用它来打印:

This page has been visited 0 times.
This page has been visited 1 time.
This page has been visited 2 times.

我试过了,但出现错误:

fn main() {
let mut count = 0;
let s1 = format!("This page has been visited {:d} {0, plural, one{time} other{times}}.", count);
println(s1);
}

error: argument used to format with `d` was attempted to not be used for formatting

最佳答案

错误消息非常困惑,但显然您不能对两个格式字符串使用相同的参数。也就是说,您不能同时为 {:d}{0, plural, one{time} other{times} 使用参数 0 (count)

可以通过在函数中传递两次参数来解决此限制:

let s1 = format!("This page has been visited {:d} {1, plural, one{time} other{times}}.", count, count);

或者,您可以使用 # 将值本身放在 plural 格式中:

let s1 = format!("This page has been visited {0, plural, one{# time} other{# times}}.", count);

关于rust - 你如何在 Rust 中使用复数方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21273648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com