about summary refs log tree commit diff
path: root/library/alloc/src/string.rs
AgeCommit message (Collapse)AuthorLines
2025-09-25Remove most `#[track_caller]` from allocating Vec methodsNoratrieb-13/+0
They cause significant binary size overhead while contributing little value. Also removes them from the wrapping String methods that do not panic.
2025-09-20docs: improve doc of some methods w/ rangestk-6/+6
2025-08-27Rollup merge of #145562 - tbu-:pr_simplify_to_string_spec, r=tgross35Matthias Krüger-50/+23
Simplify macro generating ToString implementations for `&…&str` Use deref coercion to let the compiler remove any amount of references. Also use that macro for `Cow` and `String`.
2025-08-26remove deprecated Error::description in implsMarijn Schouten-12/+2
2025-08-19Use `ToString` specialization macro also for `Cow` and `String`Tobias Bucher-16/+2
2025-08-19Simplify macro generating `ToString` implementations for `&…&str`Tobias Bucher-34/+21
Use deref coercion to let the compiler remove any amount of references.
2025-08-02Rollup merge of #144478 - joshtriplett:doc-code-formatting-prep, r=AmanieuSamuel Tardieu-2/+2
Improve formatting of doc code blocks We don't currently apply automatic formatting to doc comment code blocks. As a result, it has built up various idiosyncracies, which make such automatic formatting difficult. Some of those idiosyncracies also make things harder for human readers or other tools. This PR makes a few improvements to doc code formatting, in the hopes of making future automatic formatting easier, as well as in many cases providing net readability improvements. I would suggest reading each commit separately, as each commit contains one class of changes.
2025-07-27Remove `[T]::array_chunks(_mut)`Scott McMurray-12/+12
2025-07-25Avoid placing `// FIXME` comments inside doc code blocksJosh Triplett-2/+2
This leads tools like rustfmt to get confused, because the doc code block effectively spans two doc comments. As a result, the tools think the first code block is unclosed, and the subsequent terminator opens a new block. Move the FIXME comments outside the doc code blocks, instead.
2025-07-13update issue number for `const_trait_impl`Deadbeef-1/+1
2025-07-08Auto merge of #134628 - estebank:const-default, r=oli-obkbors-1/+2
Make `Default` const and add some `const Default` impls Full list of `impl const Default` types: - () - bool - char - std::ascii::Char - usize - u8 - u16 - u32 - u64 - u128 - i8 - i16 - i32 - i64 - i128 - f16 - f32 - f64 - f128 - std::marker::PhantomData<T> - Option<T> - std::iter::Empty<T> - std::ptr::Alignment - &[T] - &mut [T] - &str - &mut str - String - Vec<T>
2025-07-08Rollup merge of #143608 - codexarafat:fix-string-doc, r=NoratriebMatthias Krüger-1/+1
Fix in std::String docs This PR removes the word “else” from the sentence ('something else similar') in the String documentation to improve clarity. Fixes rust-lang/rust#143579.
2025-07-07Make `Default` const and add some `const Default` implsEsteban Küber-1/+2
Full list of `impl const Default` types: - () - bool - char - Cell - std::ascii::Char - usize - u8 - u16 - u32 - u64 - u128 - i8 - i16 - i32 - i64 - i128 - f16 - f32 - f64 - f128 - std::marker::PhantomData<T> - Option<T> - std::iter::Empty<T> - std::ptr::Alignment - &[T] - &mut [T] - &str - &mut str - String - Vec<T>
2025-07-08Fix in String docs: remove 'else' from 'something else similar'Md. Yeasin Arafat-1/+1
2025-07-03Implement `int_format_into` featureGuillaume Gomez-2/+4
2025-06-22Auto merge of #142728 - kornelski:string-track, r=tgross35bors-0/+23
Let String pass #[track_caller] to its Vec calls I've added `#[track_caller]` to `String` methods that delegate to `Vec` methods that already have `#[track_caller]`. I've also added `#[track_caller]` to methods that have `assert!` or `panic!` due to invalid inputs.
2025-06-20Convert `ilog(10)` to `ilog10()`Chai T. Rex-2/+2
2025-06-20Auto merge of #142294 - GuillaumeGomez:specialize-tostring-on-128-integers, ↵bors-0/+1
r=tgross35 Use a distinct `ToString` implementation for `u128` and `i128` Part of https://github.com/rust-lang/rust/issues/135543. Follow-up of rust-lang/rust#136264. When working on https://github.com/rust-lang/rust/pull/142098, I realized that `i128` and `u128` could also benefit from a distinct `ToString` implementation so here it. The last commit is just me realizing that I forgot to add the format tests for `usize` and `isize`. Here is the bench comparison: | bench name | last nightly | with this PR | diff | |-|-|-|-| | bench_i128 | 29.25 ns/iter (+/- 0.66) | 17.52 ns/iter (+/- 0.7) | -40.1% | | bench_u128 | 34.06 ns/iter (+/- 0.21) | 16.1 ns/iter (+/- 0.6) | -52.7% | I used this code to test: ```rust #![feature(test)] extern crate test; use test::{Bencher, black_box}; #[inline(always)] fn convert_to_string<T: ToString>(n: T) -> String { n.to_string() } macro_rules! decl_benches { ($($name:ident: $ty:ident,)+) => { $( #[bench] fn $name(c: &mut Bencher) { c.iter(|| convert_to_string(black_box({ let nb: $ty = 20; nb }))); } )+ } } decl_benches! { bench_u128: u128, bench_i128: i128, } ```
2025-06-19Add #[track_caller] to String methods that assert inputsKornel-0/+6
2025-06-19Let String pass #[track_caller] to its Vec callsKornel-0/+17
2025-06-17Make performance of String::insert_str more preciseMarijn Schouten-9/+16
2025-06-16Specialize `ToString` implementation on `u128` and `i128`Guillaume Gomez-0/+1
2025-05-15Auto merge of #136264 - GuillaumeGomez:optimize-integers-to-string, r=Amanieubors-0/+48
Optimize `ToString` implementation for integers Part of https://github.com/rust-lang/rust/issues/135543. Follow-up of https://github.com/rust-lang/rust/pull/133247 and https://github.com/rust-lang/rust/pull/128204. The benchmark results are: | name| 1.87.0-nightly (3ea711f17 2025-03-09) | With this PR | diff | |-|-|-|-| | bench_i16 | 32.06 ns/iter (+/- 0.12) | 17.62 ns/iter (+/- 0.03) | -45% | | bench_i32 | 31.61 ns/iter (+/- 0.04) | 15.10 ns/iter (+/- 0.06) | -52% | | bench_i64 | 31.71 ns/iter (+/- 0.07) | 15.02 ns/iter (+/- 0.20) | -52% | | bench_i8 | 13.21 ns/iter (+/- 0.14) | 14.93 ns/iter (+/- 0.16) | +13% | | bench_u16 | 31.20 ns/iter (+/- 0.06) | 16.14 ns/iter (+/- 0.11) | -48% | | bench_u32 | 33.27 ns/iter (+/- 0.05) | 16.18 ns/iter (+/- 0.10) | -51% | | bench_u64 | 31.44 ns/iter (+/- 0.06) | 16.62 ns/iter (+/- 0.21) | -47% | | bench_u8 | 10.57 ns/iter (+/- 0.30) | 13.00 ns/iter (+/- 0.43) | +22% | More information about it in [the original comment](https://github.com/rust-lang/rust/pull/136264#discussion_r1987542954). r? `@workingjubilee`
2025-05-12update cfg(bootstrap)Pietro Albini-2/+2
2025-04-20Add `#[rustc_no_implicit_autorefs]` and apply it to std methodsUrgau-0/+2
2025-04-12Optimize `ToString` implementation for integersGuillaume Gomez-0/+48
2025-04-10Auto merge of #139279 - BoxyUwU:bump-boostrap, r=jieyouxubors-10/+10
Bump boostrap compiler to new beta try-job: `*msvc*`
2025-04-09replace version placeholderBoxy-10/+10
2025-04-09Speed up `String::push` and `String::insert`lincot-18/+47
Improve performance of `String` methods by avoiding unnecessary memcpy for the character bytes, with added codegen check to ensure compliance.
2025-03-08Stabilize `const_vec_string_slice`Martin Habovstiak-8/+9
This feature was approved for stabilization in https://github.com/rust-lang/rust/issues/129041#issuecomment-2508940661 so this change stabilizes it.
2025-03-07Fully test the alloc crate through alloctestsbjorn3-21/+9
For the tests that make use of internal implementation details, we include the module to test using #[path] in alloctests now.
2025-03-07Rollup merge of #138034 - thaliaarchi:use-prelude-size-of, r=tgross35Matthias Krüger-4/+2
library: Use `size_of` from the prelude instead of imported Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80. try-job: test-various try-job: x86_64-gnu try-job: x86_64-msvc-1
2025-03-06library: Use size_of from the prelude instead of importedThalia Archibald-4/+2
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80.
2025-03-05Rollup merge of #137569 - aDotInTheVoid:for-iurii, r=ibraheemdev许杰友 Jieyou Xu (Joe)-2/+1
Stabilize `string_extend_from_within` FCP'd here: https://github.com/rust-lang/rust/issues/103806#issuecomment-2674989531. Closes #103806.
2025-02-24Rollup merge of #136775 - robertbastian:patch-2, r=AmanieuTrevor Gross-5/+2
Update `String::from_raw_parts` safety requirements These have become out of sync with `Vec::from_raw_part`'s safety requirements, and are likely to diverge again. I think it's safest to just point at `Vec`'s requirements. https://github.com/rust-lang/rust/issues/119206#issuecomment-2180116680
2025-02-24Stablize `string_extend_from_within`Alona Enraght-Moony-2/+1
2025-02-24Update string.rsRobert Bastian-2/+2
Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
2025-02-19Rollup merge of #132268 - elichai:string_try_from_vec, r=AmanieuMatthias Krüger-0/+18
Impl TryFrom<Vec<u8>> for String I think this is useful enough to have :) As a general question, is there any policy around adding "missing" trait implementations? (like adding `AsRef<T> for T` for std types), I mostly stumble upon them when using a lot of "impl Trait in argument position" like (`foo: impl Into<String>`)
2025-02-19Rollup merge of #120580 - HTGAzureX1212:HTGAzureX1212/issue-45795, r=m-ou-seMatthias Krüger-3/+5
Add `MAX_LEN_UTF8` and `MAX_LEN_UTF16` Constants This pull request adds the `MAX_LEN_UTF8` and `MAX_LEN_UTF16` constants as per #45795, gated behind the `char_max_len` feature. The constants are currently applied in the `alloc`, `core` and `std` libraries.
2025-02-16add MAX_LEN_UTF8 and MAX_LEN_UTF16 constantsHTGAzureX1212-3/+5
2025-02-09Update string.rsRobert Bastian-5/+2
2025-02-08Implement Extend<AsciiChar> for StringMatthew Zeitlin-0/+26
2025-01-31Update encode_utf16 to mention it is native endianMarijn Schouten-8/+8
2024-12-22Impl String::into_charstison-2/+185
Signed-off-by: tison <wander4096@gmail.com>
2024-12-13Remove support for specializing ToString outside the standard librarybjorn3-37/+32
This is the only trait specializable outside of the standard library. Before stabilizing specialization we will probably want to remove support for this. It was originally made specializable to allow a more efficient ToString in libproc_macro back when this way the only way to get any data out of a TokenStream. We now support getting individual tokens, so proc macros no longer need to call it as often.
2024-12-05Fixed another broken testElias Holzmann-2/+2
2024-12-05Added struct `fmt::FormattingOptions`Elias Holzmann-1/+2
This allows to build custom `std::Formatter`s at runtime. Also added some related enums and two related methods on `std::Formatter`.
2024-11-17alloc: fix `String`'s docYutaro Ohno-1/+1
2024-10-28Impl TryFrom<Vec<u8>> for StringElichai Turkel-0/+18
2024-10-17Auto merge of #130223 - LaihoE:faster_str_replace, r=thomccbors-1/+6
optimize str.replace Adds a fast path for str.replace for the ascii to ascii case. This allows for autovectorizing the code. Also should this instead be done with specialization? This way we could remove one branch. I think it is the kind of branch that is easy to predict though. Benchmark for the fast path (replace all "a" with "b" in the rust wikipedia article, using criterion) : | N | Speedup | Time New (ns) | Time Old (ns) | |----------|---------|---------------|---------------| | 2 | 2.03 | 13.567 | 27.576 | | 8 | 1.73 | 17.478 | 30.259 | | 11 | 2.46 | 18.296 | 45.055 | | 16 | 2.71 | 17.181 | 46.526 | | 37 | 4.43 | 18.526 | 81.997 | | 64 | 8.54 | 18.670 | 159.470 | | 200 | 9.82 | 29.634 | 291.010 | | 2000 | 24.34 | 81.114 | 1974.300 | | 20000 | 30.61 | 598.520 | 18318.000 | | 1000000 | 29.31 | 33458.000 | 980540.000 |