@@ -163,6 +163,24 @@ pub fn main() {
163
163
t ! ( format!( "{:?}" , 0.0 ) , "0" ) ;
164
164
165
165
166
+ // Ergonomic format_args!
167
+ t ! ( format!( "{0:x} {0:X}" , 15 ) , "f F" ) ;
168
+ t ! ( format!( "{0:x} {0:X} {}" , 15 ) , "f F 15" ) ;
169
+ // NOTE: For now the longer test cases must not be followed immediately by
170
+ // >1 empty lines, or the pretty printer will break. Since no one wants to
171
+ // touch the current pretty printer (#751), we have no choice but to work
172
+ // around it. Some of the following test cases are also affected.
173
+ t ! ( format!( "{:x}{0:X}{a:x}{:X}{1:x}{a:X}" , 13 , 14 , a=15 ) , "dDfEeF" ) ;
174
+ t ! ( format!( "{a:x} {a:X}" , a=15 ) , "f F" ) ;
175
+
176
+ // And its edge cases
177
+ t ! ( format!( "{a:.0$} {b:.0$} {0:.0$}\n {a:.c$} {b:.c$} {c:.c$}" ,
178
+ 4 , a="abcdefg" , b="hijklmn" , c=3 ) ,
179
+ "abcd hijk 4\n abc hij 3" ) ;
180
+ t ! ( format!( "{a:.*} {0} {:.*}" , 4 , 3 , "efgh" , a="abcdef" ) , "abcd 4 efg" ) ;
181
+ t ! ( format!( "{:.a$} {a} {a:#x}" , "aaaaaa" , a=2 ) , "aa 2 0x2" ) ;
182
+
183
+
166
184
// Test that pointers don't get truncated.
167
185
{
168
186
let val = usize:: MAX ;
@@ -177,6 +195,7 @@ pub fn main() {
177
195
test_write ( ) ;
178
196
test_print ( ) ;
179
197
test_order ( ) ;
198
+ test_once ( ) ;
180
199
181
200
// make sure that format! doesn't move out of local variables
182
201
let a: Box < _ > = box 3 ;
@@ -260,3 +279,17 @@ fn test_order() {
260
279
foo( ) , foo( ) , foo( ) , a=foo( ) , b=foo( ) , c=foo( ) ) ,
261
280
"1 2 4 5 3 6" . to_string( ) ) ;
262
281
}
282
+
283
+ fn test_once ( ) {
284
+ // Make sure each argument are evaluted only once even though it may be
285
+ // formatted multiple times
286
+ fn foo ( ) -> isize {
287
+ static mut FOO : isize = 0 ;
288
+ unsafe {
289
+ FOO += 1 ;
290
+ FOO
291
+ }
292
+ }
293
+ assert_eq ! ( format!( "{0} {0} {0} {a} {a} {a}" , foo( ) , a=foo( ) ) ,
294
+ "1 1 1 2 2 2" . to_string( ) ) ;
295
+ }
0 commit comments