@@ -398,9 +398,7 @@ impl Template {
398398
399399 Fragment :: Cap ( n) => match caps. submatch_text ( * n) {
400400 Some ( slice) => slice. write_to ( w) . map_err ( RenderError :: Io ) ?,
401- None => w
402- . write ( format ! ( "{{{n}}}" ) . as_bytes ( ) )
403- . map_err ( RenderError :: Io ) ?,
401+ None => 0 , // missing submatches map to an empty string
404402 } ,
405403
406404 Fragment :: Var ( from, to) => {
@@ -465,9 +463,7 @@ impl Template {
465463 . map_err ( RenderError :: Io ) ?,
466464 Fragment :: Cap ( n) => match caps. submatch_text ( * n) {
467465 Some ( slice) => slice. write_to ( w) . map_err ( RenderError :: Io ) ?,
468- None => w
469- . write ( format ! ( "{{{n}}}" ) . as_bytes ( ) )
470- . map_err ( RenderError :: Io ) ?,
466+ None => 0 , // missing submatches map to an empty string
471467 } ,
472468 Fragment :: Var ( from, to) => match ctx. render_var ( & self . raw [ * from..* to] , w) {
473469 Some ( res) => res. map_err ( RenderError :: Io ) ?,
@@ -558,7 +554,7 @@ mod tests {
558554 #[ test_case( "{1} {2}" , "foo bar" ; "both submatches" ) ]
559555 #[ test_case( "{2} {1}" , "bar foo" ; "flipped submatches" ) ]
560556 #[ test_case( "{1}\\ n{2}" , "foo\n bar" ; "submatches and newline" ) ]
561- #[ test_case( "{3}" , "{3} " ; "unknown capture" ) ]
557+ #[ test_case( "{3}" , "" ; "unknown capture" ) ]
562558 #[ test]
563559 fn render_works ( s : & str , expected : & str ) {
564560 let caps: TaggedCaptures < & str > = TaggedCaptures {
@@ -590,7 +586,7 @@ mod tests {
590586 #[ test_case( "{1} {2}" , "foo bar" ; "both submatches" ) ]
591587 #[ test_case( "{2} {1}" , "bar foo" ; "flipped submatches" ) ]
592588 #[ test_case( "{1}\\ n{2}" , "foo\n bar" ; "submatches and newline" ) ]
593- #[ test_case( "{3}" , "{3} " ; "unknown capture" ) ]
589+ #[ test_case( "{3}" , "" ; "unknown capture" ) ]
594590 #[ test_case( "{unknown}" , "from context" ; "variable without context" ) ]
595591 #[ test]
596592 fn render_with_context_works ( s : & str , expected : & str ) {
0 commit comments