5 named_argument_takes_precedence_to_captured();
6 formatting_parameters_can_be_captured();
7 capture_raw_strings_and_idents();
9 #[cfg(panic = "unwind")]
11 panic_with_single_argument_does_not_get_formatted();
12 panic_with_multiple_arguments_is_formatted();
16 fn named_argument_takes_precedence_to_captured() {
18 let s = format!("{foo}", foo = "named");
19 assert_eq!(&s, "named");
21 let s = format!("{foo}-{foo}-{foo}", foo = "named");
22 assert_eq!(&s, "named-named-named");
24 let s = format!("{}-{bar}-{foo}", "positional", bar = "named");
25 assert_eq!(&s, "positional-named-captured");
28 fn capture_raw_strings_and_idents() {
30 let s = format!(r#"The fruit is an {type}"#);
31 assert_eq!(&s, "The fruit is an apple");
33 let r#type = "orange";
34 let s = format!(r"The fruit is an {type}");
35 assert_eq!(&s, "The fruit is an orange");
38 #[cfg(panic = "unwind")]
39 fn panic_with_single_argument_does_not_get_formatted() {
40 // panic! with a single argument does not perform string formatting.
41 // RFC #2795 suggests that this may need to change so that captured arguments are formatted.
42 // For stability reasons this will need to part of an edition change.
44 #[allow(non_fmt_panics)]
45 let msg = std::panic::catch_unwind(|| {
50 assert_eq!(msg.downcast_ref::<&str>(), Some(&"{foo}"))
53 #[cfg(panic = "unwind")]
54 fn panic_with_multiple_arguments_is_formatted() {
57 let msg = std::panic::catch_unwind(|| {
58 panic!("{}-{bar}-{foo}", "positional", bar = "named");
62 assert_eq!(msg.downcast_ref::<String>(), Some(&"positional-named-captured".to_string()))
65 fn formatting_parameters_can_be_captured() {
71 let s = format!("{x:width$}");
74 let s = format!("{x:<width$}");
77 let s = format!("{x:-^width$}");
78 assert_eq!(&s, "----7----");
80 let s = format!("{x:-^width$.precision$}");
81 assert_eq!(&s, "--7.000--");