Skip to content

Commit 72a25d0

Browse files
mahmoud-moursyMark-Simulacrum
authored andcommitted
Use implicit capture syntax in format_args
This updates the standard library's documentation to use the new 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).
1 parent ba14a83 commit 72a25d0

File tree

177 files changed

+724
-734
lines changed

Some content is hidden

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

177 files changed

+724
-734
lines changed

compiler/rustc_errors/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1087,12 +1087,12 @@ impl HandlerInner {
10871087
let warnings = match self.deduplicated_warn_count {
10881088
0 => String::new(),
10891089
1 => "1 warning emitted".to_string(),
1090-
count => format!("{} warnings emitted", count),
1090+
count => format!("{count} warnings emitted"),
10911091
};
10921092
let errors = match self.deduplicated_err_count {
10931093
0 => String::new(),
10941094
1 => "aborting due to previous error".to_string(),
1095-
count => format!("aborting due to {} previous errors", count),
1095+
count => format!("aborting due to {count} previous errors"),
10961096
};
10971097
if self.treat_err_as_bug() {
10981098
return;

compiler/rustc_lint_defs/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3399,7 +3399,7 @@ declare_lint! {
33993399
/// // ^^^^^^^^
34003400
/// // This call to try_into matches both Foo:try_into and TryInto::try_into as
34013401
/// // `TryInto` has been added to the Rust prelude in 2021 edition.
3402-
/// println!("{}", x);
3402+
/// println!("{x}");
34033403
/// }
34043404
/// ```
34053405
///

compiler/rustc_middle/src/ty/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub struct CaptureInfo {
281281
/// let mut t = (0,1);
282282
///
283283
/// let c = || {
284-
/// println!("{}",t); // L1
284+
/// println!("{t}"); // L1
285285
/// t.1 = 4; // L2
286286
/// };
287287
/// ```

compiler/rustc_typeck/src/check/upvar.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
458458
/// let s: String; // hir_id_s
459459
/// let mut p: Point; // his_id_p
460460
/// let c = || {
461-
/// println!("{}", s); // L1
461+
/// println!("{s}"); // L1
462462
/// p.x += 10; // L2
463-
/// println!("{}" , p.y) // L3
464-
/// println!("{}", p) // L4
463+
/// println!("{}" , p.y); // L3
464+
/// println!("{p}"); // L4
465465
/// drop(s); // L5
466466
/// };
467467
/// ```

library/alloc/src/alloc.rs

+1-1
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]`

library/alloc/src/borrow.rs

+1-1
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
///

library/alloc/src/boxed.rs

+6-6
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))`.
@@ -1408,7 +1408,7 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
14081408
/// let slice: &[u8] = &[104, 101, 108, 108, 111];
14091409
/// let boxed_slice: Box<[u8]> = Box::from(slice);
14101410
///
1411-
/// println!("{:?}", boxed_slice);
1411+
/// println!("{boxed_slice:?}");
14121412
/// ```
14131413
fn from(slice: &[T]) -> Box<[T]> {
14141414
let len = slice.len();
@@ -1450,7 +1450,7 @@ impl From<&str> for Box<str> {
14501450
///
14511451
/// ```rust
14521452
/// let boxed: Box<str> = Box::from("hello");
1453-
/// println!("{}", boxed);
1453+
/// println!("{boxed}");
14541454
/// ```
14551455
#[inline]
14561456
fn from(s: &str) -> Box<str> {
@@ -1475,14 +1475,14 @@ impl From<Cow<'_, str>> for Box<str> {
14751475
///
14761476
/// let unboxed = Cow::Borrowed("hello");
14771477
/// let boxed: Box<str> = Box::from(unboxed);
1478-
/// println!("{}", boxed);
1478+
/// println!("{boxed}");
14791479
/// ```
14801480
///
14811481
/// ```rust
14821482
/// # use std::borrow::Cow;
14831483
/// let unboxed = Cow::Owned("hello".to_string());
14841484
/// let boxed: Box<str> = Box::from(unboxed);
1485-
/// println!("{}", boxed);
1485+
/// println!("{boxed}");
14861486
/// ```
14871487
#[inline]
14881488
fn from(cow: Cow<'_, str>) -> Box<str> {
@@ -1529,7 +1529,7 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
15291529
///
15301530
/// ```rust
15311531
/// let boxed: Box<[u8]> = Box::from([4, 2]);
1532-
/// println!("{:?}", boxed);
1532+
/// println!("{boxed:?}");
15331533
/// ```
15341534
fn from(array: [T; N]) -> Box<[T]> {
15351535
box array

library/alloc/src/collections/binary_heap.rs

+5-5
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> {

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

+6-6
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();

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

+1-1
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());

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

+2-2
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
/// ```

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

+3-3
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
}

library/alloc/src/fmt.rs

+3-3
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
//!

library/alloc/src/rc/tests.rs

+3-3
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]

library/alloc/src/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
//! ```
4949
//! let numbers = &[0, 1, 2];
5050
//! for n in numbers {
51-
//! println!("{} is a number!", n);
51+
//! println!("{n} is a number!");
5252
//! }
5353
//! ```
5454
//!

library/alloc/src/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2718,7 +2718,7 @@ impl From<String> for Vec<u8> {
27182718
/// let v1 = Vec::from(s1);
27192719
///
27202720
/// for b in v1 {
2721-
/// println!("{}", b);
2721+
/// println!("{b}");
27222722
/// }
27232723
/// ```
27242724
fn from(string: String) -> Vec<u8> {

library/alloc/src/sync.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ macro_rules! acquire {
200200
/// let five = Arc::clone(&five);
201201
///
202202
/// thread::spawn(move || {
203-
/// println!("{:?}", five);
203+
/// println!("{five:?}");
204204
/// });
205205
/// }
206206
/// ```
@@ -221,7 +221,7 @@ macro_rules! acquire {
221221
///
222222
/// thread::spawn(move || {
223223
/// let v = val.fetch_add(1, Ordering::SeqCst);
224-
/// println!("{:?}", v);
224+
/// println!("{v:?}");
225225
/// });
226226
/// }
227227
/// ```

library/alloc/src/sync/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ fn test_weak_count() {
335335
#[test]
336336
fn show_arc() {
337337
let a = Arc::new(5);
338-
assert_eq!(format!("{:?}", a), "5");
338+
assert_eq!(format!("{a:?}"), "5");
339339
}
340340

341341
// Make sure deriving works with Arc<T>
@@ -347,7 +347,7 @@ struct Foo {
347347
#[test]
348348
fn test_unsized() {
349349
let x: Arc<[i32]> = Arc::new([1, 2, 3]);
350-
assert_eq!(format!("{:?}", x), "[1, 2, 3]");
350+
assert_eq!(format!("{x:?}"), "[1, 2, 3]");
351351
let y = Arc::downgrade(&x.clone());
352352
drop(x);
353353
assert!(y.upgrade().is_none());
@@ -359,7 +359,7 @@ fn test_maybe_thin_unsized() {
359359
use std::ffi::{CStr, CString};
360360

361361
let x: Arc<CStr> = Arc::from(CString::new("swordfish").unwrap().into_boxed_c_str());
362-
assert_eq!(format!("{:?}", x), "\"swordfish\"");
362+
assert_eq!(format!("{x:?}"), "\"swordfish\"");
363363
let y: Weak<CStr> = Arc::downgrade(&x);
364364
drop(x);
365365

@@ -509,7 +509,7 @@ fn test_from_box_trait_zero_sized() {
509509
let b: Box<dyn Debug> = box ();
510510
let r: Arc<dyn Debug> = Arc::from(b);
511511

512-
assert_eq!(format!("{:?}", r), "()");
512+
assert_eq!(format!("{r:?}"), "()");
513513
}
514514

515515
#[test]

library/alloc/src/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ fn any_move() {
4747
fn test_show() {
4848
let a = Box::new(8) as Box<dyn Any>;
4949
let b = Box::new(Test) as Box<dyn Any>;
50-
let a_str = format!("{:?}", a);
51-
let b_str = format!("{:?}", b);
50+
let a_str = format!("{a:?}");
51+
let b_str = format!("{b:?}");
5252
assert_eq!(a_str, "Any { .. }");
5353
assert_eq!(b_str, "Any { .. }");
5454

5555
static EIGHT: usize = 8;
5656
static TEST: Test = Test;
5757
let a = &EIGHT as &dyn Any;
5858
let b = &TEST as &dyn Any;
59-
let s = format!("{:?}", a);
59+
let s = format!("{a:?}");
6060
assert_eq!(s, "Any { .. }");
61-
let s = format!("{:?}", b);
61+
let s = format!("{b:?}");
6262
assert_eq!(s, "Any { .. }");
6363
}
6464

0 commit comments

Comments
 (0)