Skip to content

Commit 2e8f781

Browse files
authored
Rollup merge of rust-lang#93950 - T-O-R-U-S:use-modern-formatting-for-format!-macros, r=Mark-Simulacrum
Use modern formatting for format! macros This updates the standard library's documentation to use the new format_args syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored). `eprintln!("{}", e)` becomes `eprintln!("{e}")`, but `eprintln!("{}", e.kind())` remains untouched.
2 parents 5c16eb2 + 433aa0c commit 2e8f781

File tree

155 files changed

+655
-663
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+655
-663
lines changed

alloc/src/alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ pub mod __alloc_error_handler {
397397
// if there is no `#[alloc_error_handler]`
398398
#[rustc_std_internal_symbol]
399399
pub unsafe extern "C-unwind" fn __rdl_oom(size: usize, _align: usize) -> ! {
400-
panic!("memory allocation of {} bytes failed", size)
400+
panic!("memory allocation of {size} bytes failed")
401401
}
402402

403403
// if there is an `#[alloc_error_handler]`

alloc/src/borrow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ where
161161
/// let readonly = [1, 2];
162162
/// let borrowed = Items::new((&readonly[..]).into());
163163
/// match borrowed {
164-
/// Items { values: Cow::Borrowed(b) } => println!("borrowed {:?}", b),
164+
/// Items { values: Cow::Borrowed(b) } => println!("borrowed {b:?}"),
165165
/// _ => panic!("expect borrowed value"),
166166
/// }
167167
///

alloc/src/boxed.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! }
3232
//!
3333
//! let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
34-
//! println!("{:?}", list);
34+
//! println!("{list:?}");
3535
//! ```
3636
//!
3737
//! This will print `Cons(1, Cons(2, Nil))`.
@@ -1407,7 +1407,7 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
14071407
/// let slice: &[u8] = &[104, 101, 108, 108, 111];
14081408
/// let boxed_slice: Box<[u8]> = Box::from(slice);
14091409
///
1410-
/// println!("{:?}", boxed_slice);
1410+
/// println!("{boxed_slice:?}");
14111411
/// ```
14121412
fn from(slice: &[T]) -> Box<[T]> {
14131413
let len = slice.len();
@@ -1449,7 +1449,7 @@ impl From<&str> for Box<str> {
14491449
///
14501450
/// ```rust
14511451
/// let boxed: Box<str> = Box::from("hello");
1452-
/// println!("{}", boxed);
1452+
/// println!("{boxed}");
14531453
/// ```
14541454
#[inline]
14551455
fn from(s: &str) -> Box<str> {
@@ -1474,14 +1474,14 @@ impl From<Cow<'_, str>> for Box<str> {
14741474
///
14751475
/// let unboxed = Cow::Borrowed("hello");
14761476
/// let boxed: Box<str> = Box::from(unboxed);
1477-
/// println!("{}", boxed);
1477+
/// println!("{boxed}");
14781478
/// ```
14791479
///
14801480
/// ```rust
14811481
/// # use std::borrow::Cow;
14821482
/// let unboxed = Cow::Owned("hello".to_string());
14831483
/// let boxed: Box<str> = Box::from(unboxed);
1484-
/// println!("{}", boxed);
1484+
/// println!("{boxed}");
14851485
/// ```
14861486
#[inline]
14871487
fn from(cow: Cow<'_, str>) -> Box<str> {
@@ -1528,7 +1528,7 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
15281528
///
15291529
/// ```rust
15301530
/// let boxed: Box<[u8]> = Box::from([4, 2]);
1531-
/// println!("{:?}", boxed);
1531+
/// println!("{boxed:?}");
15321532
/// ```
15331533
fn from(array: [T; N]) -> Box<[T]> {
15341534
box array

alloc/src/collections/binary_heap.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ use super::SpecExtend;
194194
/// // We can iterate over the items in the heap, although they are returned in
195195
/// // a random order.
196196
/// for x in &heap {
197-
/// println!("{}", x);
197+
/// println!("{x}");
198198
/// }
199199
///
200200
/// // If we instead pop these scores, they should come back in order.
@@ -830,7 +830,7 @@ impl<T> BinaryHeap<T> {
830830
///
831831
/// // Print 1, 2, 3, 4 in arbitrary order
832832
/// for x in heap.iter() {
833-
/// println!("{}", x);
833+
/// println!("{x}");
834834
/// }
835835
/// ```
836836
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1110,7 +1110,7 @@ impl<T> BinaryHeap<T> {
11101110
///
11111111
/// // Will print in some order
11121112
/// for x in vec {
1113-
/// println!("{}", x);
1113+
/// println!("{x}");
11141114
/// }
11151115
/// ```
11161116
#[must_use = "`self` will be dropped if the result is not used"]
@@ -1179,7 +1179,7 @@ impl<T> BinaryHeap<T> {
11791179
/// assert!(!heap.is_empty());
11801180
///
11811181
/// for x in heap.drain() {
1182-
/// println!("{}", x);
1182+
/// println!("{x}");
11831183
/// }
11841184
///
11851185
/// assert!(heap.is_empty());
@@ -1624,7 +1624,7 @@ impl<T> IntoIterator for BinaryHeap<T> {
16241624
/// // Print 1, 2, 3, 4 in arbitrary order
16251625
/// for x in heap.into_iter() {
16261626
/// // x has type i32, not &i32
1627-
/// println!("{}", x);
1627+
/// println!("{x}");
16281628
/// }
16291629
/// ```
16301630
fn into_iter(self) -> IntoIter<T> {

alloc/src/collections/btree/map.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT;
104104
/// let to_find = ["Up!", "Office Space"];
105105
/// for movie in &to_find {
106106
/// match movie_reviews.get(movie) {
107-
/// Some(review) => println!("{}: {}", movie, review),
108-
/// None => println!("{} is unreviewed.", movie)
107+
/// Some(review) => println!("{movie}: {review}"),
108+
/// None => println!("{movie} is unreviewed.")
109109
/// }
110110
/// }
111111
///
@@ -114,7 +114,7 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT;
114114
///
115115
/// // iterate over everything.
116116
/// for (movie, review) in &movie_reviews {
117-
/// println!("{}: \"{}\"", movie, review);
117+
/// println!("{movie}: \"{review}\"");
118118
/// }
119119
/// ```
120120
///
@@ -1061,7 +1061,7 @@ impl<K, V> BTreeMap<K, V> {
10611061
/// map.insert(5, "b");
10621062
/// map.insert(8, "c");
10631063
/// for (&key, &value) in map.range((Included(&4), Included(&8))) {
1064-
/// println!("{}: {}", key, value);
1064+
/// println!("{key}: {value}");
10651065
/// }
10661066
/// assert_eq!(Some((&5, &"b")), map.range(4..).next());
10671067
/// ```
@@ -1104,7 +1104,7 @@ impl<K, V> BTreeMap<K, V> {
11041104
/// *balance += 100;
11051105
/// }
11061106
/// for (name, balance) in &map {
1107-
/// println!("{} => {}", name, balance);
1107+
/// println!("{name} => {balance}");
11081108
/// }
11091109
/// ```
11101110
#[stable(feature = "btree_range", since = "1.17.0")]
@@ -2088,7 +2088,7 @@ impl<K, V> BTreeMap<K, V> {
20882088
/// map.insert(1, "a");
20892089
///
20902090
/// for (key, value) in map.iter() {
2091-
/// println!("{}: {}", key, value);
2091+
/// println!("{key}: {value}");
20922092
/// }
20932093
///
20942094
/// let (first_key, first_value) = map.iter().next().unwrap();

alloc/src/collections/btree/map/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,7 @@ fn test_ord_absence() {
17581758
}
17591759

17601760
fn map_debug<K: Debug>(mut map: BTreeMap<K, ()>) {
1761-
format!("{:?}", map);
1761+
format!("{map:?}");
17621762
format!("{:?}", map.iter());
17631763
format!("{:?}", map.iter_mut());
17641764
format!("{:?}", map.keys());

alloc/src/collections/btree/set.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use super::Recover;
6060
///
6161
/// // Iterate over everything.
6262
/// for book in &books {
63-
/// println!("{}", book);
63+
/// println!("{book}");
6464
/// }
6565
/// ```
6666
///
@@ -284,7 +284,7 @@ impl<T> BTreeSet<T> {
284284
/// set.insert(5);
285285
/// set.insert(8);
286286
/// for &elem in set.range((Included(&4), Included(&8))) {
287-
/// println!("{}", elem);
287+
/// println!("{elem}");
288288
/// }
289289
/// assert_eq!(Some(&5), set.range(4..).next());
290290
/// ```

alloc/src/collections/btree/set/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,10 @@ fn test_show() {
431431
set.insert(1);
432432
set.insert(2);
433433

434-
let set_str = format!("{:?}", set);
434+
let set_str = format!("{set:?}");
435435

436436
assert_eq!(set_str, "{1, 2}");
437-
assert_eq!(format!("{:?}", empty), "{}");
437+
assert_eq!(format!("{empty:?}"), "{}");
438438
}
439439

440440
#[test]
@@ -649,7 +649,7 @@ fn test_ord_absence() {
649649
}
650650

651651
fn set_debug<K: Debug>(set: BTreeSet<K>) {
652-
format!("{:?}", set);
652+
format!("{set:?}");
653653
format!("{:?}", set.iter());
654654
format!("{:?}", set.into_iter());
655655
}

alloc/src/fmt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,9 @@
416416
//! fn main() {
417417
//! let myvector = Vector2D { x: 3, y: 4 };
418418
//!
419-
//! println!("{}", myvector); // => "(3, 4)"
420-
//! println!("{:?}", myvector); // => "Vector2D {x: 3, y:4}"
421-
//! println!("{:10.3b}", myvector); // => " 5.000"
419+
//! println!("{myvector}"); // => "(3, 4)"
420+
//! println!("{myvector:?}"); // => "Vector2D {x: 3, y:4}"
421+
//! println!("{myvector:10.3b}"); // => " 5.000"
422422
//! }
423423
//! ```
424424
//!

alloc/src/rc/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ fn test_cowrc_clone_weak() {
309309
#[test]
310310
fn test_show() {
311311
let foo = Rc::new(75);
312-
assert_eq!(format!("{:?}", foo), "75");
312+
assert_eq!(format!("{foo:?}"), "75");
313313
}
314314

315315
#[test]
@@ -324,7 +324,7 @@ fn test_maybe_thin_unsized() {
324324
use std::ffi::{CStr, CString};
325325

326326
let x: Rc<CStr> = Rc::from(CString::new("swordfish").unwrap().into_boxed_c_str());
327-
assert_eq!(format!("{:?}", x), "\"swordfish\"");
327+
assert_eq!(format!("{x:?}"), "\"swordfish\"");
328328
let y: Weak<CStr> = Rc::downgrade(&x);
329329
drop(x);
330330

@@ -451,7 +451,7 @@ fn test_from_box_trait_zero_sized() {
451451
let b: Box<dyn Debug> = box ();
452452
let r: Rc<dyn Debug> = Rc::from(b);
453453

454-
assert_eq!(format!("{:?}", r), "()");
454+
assert_eq!(format!("{r:?}"), "()");
455455
}
456456

457457
#[test]

0 commit comments

Comments
 (0)