π Interactive Rust Notation Guide
π Notation Quick Reference
IDENTIFIER
XID_Start XID_Continue* | _ XID_Continue+
Variable and function names, with Unicode support
? (Optional)
PATTERN?
Element appears zero or one times
* (Kleene Star)
PATTERN*
Element appears zero or more times
+ (Plus)
PATTERN+
Element appears one or more times
| (Alternative)
PATTERN | PATTERN
Either one pattern or another
( ) (Grouping)
(PATTERN PATTERN)
Groups elements together
π― Understanding Rust Grammar Notation
Rust's grammar notation is based on Extended Backus-Naur Form (EBNF), a standardized way to express context-free grammars. This guide will help you decode the notation used throughout the Rust reference and understand how grammar rules translate to actual code.
IDENTIFIER Grammar
XID_Start
XID_Continue*
|
_
XID_Continue+
FUNCTION Grammar
fn
IDENTIFIER
(
PARAMETERS
)
-> TYPE
BLOCK
π€ Basic Notation Elements
Grammar Notation
PATTERN? // Optional (0 or 1)
PATTERN* // Kleene star (0 or more)
PATTERN+ // Plus (1 or more)
A | B // Alternative (A or B)
(A B) // Grouping
What It Means
Maybe there, maybe not
Can repeat any number of times
Must appear at least once
Pick one option
Group elements together
π Advanced Pattern Matching
β
Complex Grammar Rule
ForExpression:
'for' Pattern 'in' Expression BlockExpression
This rule defines how for loops work in Rust.
β
Corresponding Rust Code
for item in collection {
println!("{}", item);
}
π Real-World Examples
π¨ Visual Grammar Breakdown
Let's break down a complex grammar rule step by step:
IF Expression Complete
if
Expression
Block
else (Block | IfExpression)
Simple If
if condition {
do_something();
}
If-Else Chain
if condition {
do_something();
} else if other {
do_other();
} else {
default_action();
}
π€ For AI Coding Agents
{
"rust_notation_system": {
"core_operators": {
"optional": {
"symbol": "?",
"meaning": "zero_or_one_occurrence",
"example": "Pattern?",
"usage": "Type annotations, optional parameters"
},
"kleene_star": {
"symbol": "*",
"meaning": "zero_or_more_occurrences",
"example": "Pattern*",
"usage": "Repeated elements, lists"
},
"plus": {
"symbol": "+",
"meaning": "one_or_more_occurrences",
"example": "Pattern+",
"usage": "Non-empty sequences"
},
"alternative": {
"symbol": "|",
"meaning": "exclusive_choice",
"example": "A | B",
"usage": "Enum variants, type choices"
},
"grouping": {
"symbol": "( )",
"meaning": "precedence_grouping",
"example": "(A B)+",
"usage": "Complex pattern combinations"
}
},
"terminal_symbols": {
"literals": ["'keyword'", "\"string\"", "'char'"],
"categories": ["IDENTIFIER", "INTEGER", "STRING"],
"description": "Exact text that must appear in source"
},
"non_terminal_symbols": {
"pattern_names": ["Expression", "Statement", "Item"],
"description": "References to other grammar rules"
},
"parsing_precedence": {
"highest": "( )",
"high": "? * +",
"medium": "concatenation",
"lowest": "|"
},
"common_patterns": {
"optional_trailing_comma": "( ',' Pattern )* ','?",
"identifier_list": "IDENTIFIER ( ',' IDENTIFIER )*",
"generic_params": "'<' ( TypeParam ( ',' TypeParam )* )? '>'"
}
}
}
π Core Notation Elements
π Practical Grammar Reading
Unicode Identifiers
Rust uses Unicode XID_Start and XID_Continue categories, not just ASCII letters:
let ει = 42; // Chinese characters
let ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ = 100; // Cyrillic characters
let ΞΌ = 3.14159; // Greek letter mu
Raw Identifiers
Use r# to use keywords as identifiers:
let r#fn = "function";
let r#match = "matching";
Trailing Commas
Most comma-separated lists allow optional trailing commas:
let array = [1, 2, 3,]; // Trailing comma OK
function_call(a, b, c,); // Also OK here
π Common Grammar Patterns
π Recurring Patterns
Comma-Separated Lists
ITEM ( ',' ITEM )* ','?
Function parameters, array elements, etc.
Optional Generic Parameters
'<' ( TypeParam ( ',' TypeParam )* )? '>'
Generic type parameters
Block with Statements
'{' Statement* Expression? '}'
Function bodies, if blocks, etc.
Path Segments
IDENTIFIER ( '::' IDENTIFIER )*
Module paths, type paths
β‘ Advanced Topics
π Practice Exercises
Try to decode these grammar rules and predict what Rust code would match: