New format_args!() and fmt::Arguments implementation Part of https://github.com/rust-lang/rust/issues/99012 This is a new implementation of `format_args!()` and `fmt::Arguments`. With this implementation, `fmt::Arguments` is only two pointers in size. (Instead of six, before.) This makes it the same size as a `&str` and makes it fit in a register pair. --- This `fmt::Arguments` can store a `&'static str` _without any indirection_ or additional storage. This means that simple cases like `print_fmt(format_args!("hello"))` are now just as efficient for the caller as `print_str("hello")`, as shown by this example: > code: > ```rust > fn main() { > println!("Hello, world!"); > } > ``` > > before: > ```asm > main: > sub rsp, 56 > lea rax, [rip + .Lanon_hello_world] > mov qword ptr [rsp + 8], rax > mov qword ptr [rsp + 16], 1 > mov qword ptr [rsp + 24], 8 > xorps xmm0, xmm0 > movups xmmword ptr [rsp + 32], xmm0 > lea rdi, [rsp + 8] > call qword ptr [rip + std::io::stdio::_print] > add rsp, 56 > ret > ``` > > after: > ```asm > main: > lea rsi, [rip + .Lanon_hello_world] > mov edi, 29 > jmp qword ptr [rip + std::io::stdio::_print] > ``` (`panic!("Hello, world!");` shows a similar change.) --- This implementation stores all static information as just a single (byte) string, without any indirection: > code: > ```rust > format_args!("Hello, {name:-^20}!") > ``` > > lowering before: > ```rust > fmt::Arguments::new_v1_formatted( > &["Hello, ", "!\n"], > &args, > &[ > Placeholder { > position: 0usize, > flags: 3355443245u32, > precision: format_count::Implied, > width: format_count::Is(20u16), > }, > ], > ) > ``` > > lowering after: > ```rust > fmt::Arguments::new( > b"\x07Hello, \xc3-\x00\x00\xc8\x14\x00\x02!\n\x00", > &args, > ) > ``` This saves a ton of pointers and simplifies the expansion significantly, but does mean that individual pieces (e.g. `"Hello, "` and `"!\n"`) cannot be reused. (Those pieces are often smaller than a pointer to them, though, in which case reusing them is useless.) --- The details of the new representation are documented in [library/core/src/fmt/mod.rs](https://github.com/m-ou-se/rust/blob/new-fmt-args-alt/library/core/src/fmt/mod.rs#L609-L712). |
||
|---|---|---|
| .github | ||
| compiler | ||
| library | ||
| LICENSES | ||
| src | ||
| tests | ||
| .clang-format | ||
| .editorconfig | ||
| .git-blame-ignore-revs | ||
| .gitattributes | ||
| .gitignore | ||
| .gitmodules | ||
| .ignore | ||
| .mailmap | ||
| bootstrap.example.toml | ||
| Cargo.lock | ||
| Cargo.toml | ||
| CODE_OF_CONDUCT.md | ||
| configure | ||
| CONTRIBUTING.md | ||
| COPYRIGHT | ||
| INSTALL.md | ||
| LICENSE-APACHE | ||
| license-metadata.json | ||
| LICENSE-MIT | ||
| package-lock.json | ||
| package.json | ||
| README.md | ||
| RELEASES.md | ||
| REUSE.toml | ||
| rust-bors.toml | ||
| rustfmt.toml | ||
| triagebot.toml | ||
| typos.toml | ||
| x | ||
| x.ps1 | ||
| x.py | ||
This is the main source code repository for Rust. It contains the compiler, standard library, and documentation.
Why Rust?
-
Performance: Fast and memory-efficient, suitable for critical services, embedded devices, and easily integrated with other languages.
-
Reliability: Our rich type system and ownership model ensure memory and thread safety, reducing bugs at compile-time.
-
Productivity: Comprehensive documentation, a compiler committed to providing great diagnostics, and advanced tooling including package manager and build tool (Cargo), auto-formatter (rustfmt), linter (Clippy) and editor support (rust-analyzer).
Quick Start
Read "Installation" from The Book.
Installing from Source
If you really want to install from source (though this is not recommended), see INSTALL.md.
Getting Help
See https://www.rust-lang.org/community for a list of chat platforms and forums.
Contributing
See CONTRIBUTING.md.
License
Rust is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.
See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.
Trademark
The Rust Foundation owns and protects the Rust and Cargo trademarks and logos (the "Rust Trademarks").
If you want to use these names or brands, please read the Rust language trademark policy.
Third-party logos may be subject to third-party copyrights and trademarks. See Licenses for details.