番号:
私はこれに似た何かをしたいと思います。その後、MustUse<f64>
を返すことができる、と彼らはcalculate_the_thing(12.3)
を書いた場合、ユーザーは警告が表示されます、でも取得するための正しい方法を示唆
#[must_use = "this value should be used (extract with .0)"]
pub struct MustUse<T>(pub T);
あなたの機能:
1つの代替は#[must_use]
単純なラッパー型を定義することです彼らが望むもの:let x = calculate_the_thing(12.3).0;
。 For instance:
fn calculate_the_thing(number: f64) -> MustUse<f64> {
MustUse(number * 2.0)
}
fn main() {
calculate_the_thing(12.3); // whoops
let x = calculate_the_thing(12.3).0;
println!("{}", x);
}
<anon>:9:5: 9:31 warning: unused result which must be used: this value should be used (extract with .0), #[warn(unused_must_use)] on by default
<anon>:9 calculate_the_thing(12.3); // whoops
^~~~~~~~~~~~~~~~~~~~~~~~~~
私は#[must_use]
を拡張する提案RFC 886を書いたが、それは十分にやる気として拒否されました。
* .0 *で抽出 - それは私が期待していない滑らかさの少しです。とてもかっこいい! – Shepmaster
[Write :: write'](https://doc.rust-lang.org/std/io/trait.Write.html#tymethod.write)から返された、書き込まれたバイト数が、これは無視され、すべてのデータが書かれていると仮定すると、断続的なバグになる可能性が高いです。 – poolie
悲しみ。 RFC 886はすばらしいでしょう - (私が実際に探していたもの)。 :( – U007D