2019-10-12 09:07:13 -05:00
|
|
|
// run-pass
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
named_argument_takes_precedence_to_captured();
|
|
|
|
formatting_parameters_can_be_captured();
|
2021-02-06 09:01:07 -06:00
|
|
|
capture_raw_strings_and_idents();
|
2022-01-27 16:59:34 -06:00
|
|
|
repeated_capture();
|
2020-07-25 13:02:49 -05:00
|
|
|
|
|
|
|
#[cfg(panic = "unwind")]
|
|
|
|
{
|
|
|
|
panic_with_single_argument_does_not_get_formatted();
|
|
|
|
panic_with_multiple_arguments_is_formatted();
|
|
|
|
}
|
2019-10-12 09:07:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn named_argument_takes_precedence_to_captured() {
|
|
|
|
let foo = "captured";
|
2021-06-29 13:33:31 -05:00
|
|
|
let s = format!("{foo}", foo = "named");
|
2019-10-12 09:07:13 -05:00
|
|
|
assert_eq!(&s, "named");
|
|
|
|
|
2021-06-29 13:33:31 -05:00
|
|
|
let s = format!("{foo}-{foo}-{foo}", foo = "named");
|
2019-10-12 09:07:13 -05:00
|
|
|
assert_eq!(&s, "named-named-named");
|
|
|
|
|
2021-06-29 13:33:31 -05:00
|
|
|
let s = format!("{}-{bar}-{foo}", "positional", bar = "named");
|
2019-10-12 09:07:13 -05:00
|
|
|
assert_eq!(&s, "positional-named-captured");
|
|
|
|
}
|
|
|
|
|
2021-02-06 09:01:07 -06:00
|
|
|
fn capture_raw_strings_and_idents() {
|
|
|
|
let r#type = "apple";
|
|
|
|
let s = format!(r#"The fruit is an {type}"#);
|
|
|
|
assert_eq!(&s, "The fruit is an apple");
|
|
|
|
|
|
|
|
let r#type = "orange";
|
|
|
|
let s = format!(r"The fruit is an {type}");
|
|
|
|
assert_eq!(&s, "The fruit is an orange");
|
|
|
|
}
|
|
|
|
|
2020-07-25 13:02:49 -05:00
|
|
|
#[cfg(panic = "unwind")]
|
2019-10-12 09:07:13 -05:00
|
|
|
fn panic_with_single_argument_does_not_get_formatted() {
|
|
|
|
// panic! with a single argument does not perform string formatting.
|
|
|
|
// RFC #2795 suggests that this may need to change so that captured arguments are formatted.
|
|
|
|
// For stability reasons this will need to part of an edition change.
|
|
|
|
|
2021-06-29 13:33:31 -05:00
|
|
|
#[allow(non_fmt_panics)]
|
2019-10-12 09:07:13 -05:00
|
|
|
let msg = std::panic::catch_unwind(|| {
|
|
|
|
panic!("{foo}");
|
2021-06-29 13:33:31 -05:00
|
|
|
})
|
|
|
|
.unwrap_err();
|
2019-10-12 09:07:13 -05:00
|
|
|
|
|
|
|
assert_eq!(msg.downcast_ref::<&str>(), Some(&"{foo}"))
|
|
|
|
}
|
|
|
|
|
2020-07-25 13:02:49 -05:00
|
|
|
#[cfg(panic = "unwind")]
|
2019-10-12 09:07:13 -05:00
|
|
|
fn panic_with_multiple_arguments_is_formatted() {
|
|
|
|
let foo = "captured";
|
|
|
|
|
|
|
|
let msg = std::panic::catch_unwind(|| {
|
2021-06-29 13:33:31 -05:00
|
|
|
panic!("{}-{bar}-{foo}", "positional", bar = "named");
|
|
|
|
})
|
|
|
|
.unwrap_err();
|
2019-10-12 09:07:13 -05:00
|
|
|
|
|
|
|
assert_eq!(msg.downcast_ref::<String>(), Some(&"positional-named-captured".to_string()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn formatting_parameters_can_be_captured() {
|
|
|
|
let width = 9;
|
|
|
|
let precision = 3;
|
|
|
|
|
|
|
|
let x = 7.0;
|
|
|
|
|
|
|
|
let s = format!("{x:width$}");
|
|
|
|
assert_eq!(&s, " 7");
|
|
|
|
|
|
|
|
let s = format!("{x:<width$}");
|
|
|
|
assert_eq!(&s, "7 ");
|
|
|
|
|
|
|
|
let s = format!("{x:-^width$}");
|
|
|
|
assert_eq!(&s, "----7----");
|
|
|
|
|
|
|
|
let s = format!("{x:-^width$.precision$}");
|
|
|
|
assert_eq!(&s, "--7.000--");
|
|
|
|
}
|
2022-01-27 16:59:34 -06:00
|
|
|
|
|
|
|
fn repeated_capture() {
|
|
|
|
let a = 1;
|
|
|
|
let b = 2;
|
|
|
|
let s = format!("{a} {b} {a}");
|
|
|
|
assert_eq!(&s, "1 2 1");
|
|
|
|
}
|