← Debugging Guides

阅读中文版本

Rust — E0106 unexpected closing delimiter or mismatched braces

Locate unmatched delimiters in Rust by isolating the region and reading the compiler suggestion.

beginnerdebuggingrustsyntax

Symptoms

  • error: mismatched closing delimiter
  • error: unexpected closing delimiter `}`
  • The error points at a line far from the real mistake
  • A macro invocation with parentheses fails to parse

Possible causes

  • A missing or extra brace, bracket or parenthesis
  • A string or char literal containing an unbalanced delimiter
  • A macro uses delimiters the parser expects to be balanced differently
  • A match arm or block was not closed

Diagnosis steps

Read the replaced or suggested delimiter

cargo build 2>&1 | head -30

Interpretation

rustc often shows the expected delimiter and where the block it opened should close.

Isolate the region

rustc --edition 2021 --crate-type lib src/lib.rs 2>&1 | head

Interpretation

Compiling a single file reduces the search to one file, and formatting often fixes the indentation cue.

Run the formatter

cargo fmt -- --check

Interpretation

The formatter fails where delimiters are unbalanced and can show the offending region precisely.

Root cause

The parser found a closing delimiter that did not match the most recently opened one, because a delimiter is missing, extra, or hidden inside a literal, so blocks did not nest correctly.

Fix

Balance the delimiters by adding the missing closer or removing the extra one, and check string and char literals that may contain unbalanced delimiters so they do not confuse the parser.

Verify the fix

Recompile and confirm the parse error is gone, and run the formatter and tests to confirm the structure is valid.

Symptom walkthrough

The compiler reports a mismatched or unexpected closing delimiter, sometimes pointing at a line far from the actual mistake. The parser closes the most recent unterminated block, so a missing brace early in a function makes a later brace look unexpected. A delimiter inside a string or char literal can also unbalance a region even though it visually looks fine.

Fix in practice

Find the block that is missing or has an extra delimiter and balance it, then scan literals in the region for delimiters the parser would count. Running the formatter helps, since it fails precisely where nesting breaks, while recompiling confirms the fix.

Verification

Recompile and confirm the parse error is gone, then run the formatter check and the tests. A clean parse with formatting passing confirms the delimiters are balanced. Committing formatting as part of the style also keeps the structure readable during reviews.

Diagnostic commands

Check the behavior with commands like these:

cargo build 2>&1 | head -30
rustc --edition 2021 --crate-type lib src/lib.rs 2>&1 | head
cargo fmt -- --check