1
如何在某些標誌上使條件成爲const
字符串的一部分?如何格式化常量字符串
#[cfg(target_os = "macos")]
const OS: &'static str = "OSx";
#[cfg(target_os = "windows")]
const OS: &'static str = "Windows";
const SOME_STRING: &'static str = format!("this os is {}", OS);
此代碼不能編譯,因爲format
宏返回一個String
。我希望能夠在不進行任何分配的情況下進行格式化。有沒有可能做到不使整個字符串有條件?
'const fn'是否也可以允許'const sqrt_of_two:f64 =(2.0).sqrt()'這樣的東西成爲可能? – LinearZoetrope
你可以通過濫用宏來實現它......但這不太可能是值得的。 *真的*不太可能。 –
@Jsor:也許吧。目前還不清楚Rust的編譯時間功能評估(CTFE)最終會有多少。就我個人而言,我在「自給自足的一切」一面,但我不是在主張:) –