4 ms·
f-strings would be part of Rust's grammar. `format` macro isn't available in `core` crate, but it is in `alloc` crate, so it needs to allocate memory. f-string
by megumax 5y ago
f-strings would be part of Rust's grammar. `format` macro isn't available in `core` crate, but it is in `alloc` crate, so it needs to allocate memory.
f-strings on other hand would be pretty weird. Would `f"5"` return a &'static str or a String?
- gefhfffh 5y agoI know it would need to allocate memory, but from a user perspective that allocation isn't less visible when using f strings instead of the format macro. In your example I would return a String and generate a warning, because the f of the string isn't used.
- tialaramex 5y agoIf it was part of Rust the language it can't create Strings because String isn't part of the language, nor even a "langitem". The core Rust language has no idea there's such a thing as a String str (and thus &str) is part of the language, it's a built-in primitive type like i64 or bool, but String is just a struct the alloc crate brings into existence and so it may not be available.
- gefhfffh 5y agoI see. Though could this work by the language feature just working like a Macro? Code without alloc using f strings just wouldn't compile then
- kibwen 5y agoIn theory f-strings could act as the format_args! macro, not the format! macro. While the latter produces a String, the former produces a std::fmt::Arguments, and does not need to allocate memory (it lives in core).