Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The Language

This section is a work-in-progress experiment about making the book executable.

In this section we define the syntactic components of the Rust language, along with their grammar. For now this describes only a very small subset of the full language.

A program consists in a list of items.

Syntax
Program: items=Item*
    => Program { items }

#![allow(unused)]
fn main() {
pub struct Program {
    pub items: Vec<Item>,
}

}

Misc

Some syntactic elements we haven’t fleshed out yet.

Syntax
Identifier: IDENTIFIER
    => IDENTIFIER

Identifier IDENTIFIER
#![allow(unused)]
fn main() {
pub type Identifier = String;

}
Syntax
SimplePath: path=IDENTIFIER
    => path

SimplePath path IDENTIFIER
#![allow(unused)]
fn main() {
pub type Path = Identifier;

}
Syntax
Abi: STRING_LITERAL
    => STRING_LITERAL

Abi STRING_LITERAL
#![allow(unused)]
fn main() {
pub type Abi = String;

}
Syntax
Mutability: is_mut=mut?
    => if is_mut.is_some() { Mutability::Mutable } else { Mutability::Immutable }

Mutability is_mut mut
#![allow(unused)]
fn main() {
pub enum Mutability {
    Mutable,
    Immutable,
}

}
Syntax
GenericParams: UNSUPPORTED
    => GenericParams {}

GenericParams UNSUPPORTED
#![allow(unused)]
fn main() {
pub struct GenericParams {}

}
Syntax
WhereClauses: UNSUPPORTED
    => WhereClauses {}

WhereClauses UNSUPPORTED
#![allow(unused)]
fn main() {
pub struct WhereClauses {}

}
Syntax
OuterAttribute: UNSUPPORTED
    => OuterAttribute {}

OuterAttribute UNSUPPORTED
#![allow(unused)]
fn main() {
pub struct OuterAttribute {}

}
Syntax
InnerAttribute: UNSUPPORTED
    => InnerAttribute {}

InnerAttribute UNSUPPORTED
#![allow(unused)]
fn main() {
pub struct InnerAttribute {}

}
Syntax
Lifetime: LIFETIME
    => Lifetime {}

Lifetime LIFETIME
#![allow(unused)]
fn main() {
pub struct Lifetime {}

}
Syntax
Visibility:
    | pub => Visibility::Pub
    | pub ( crate ) => Visibility::PubCrate
    | pub ( self ) => Visibility::PubSelf
    | pub ( super ) => Visibility::PubSuper
    | pub ( in path=SimplePath ) => Visibility::InPath(path)

Visibility pub pub ( crate ) pub ( self ) pub ( super ) pub ( in path SimplePath )
#![allow(unused)]
fn main() {
pub enum Visibility {
    Pub,
    PubCrate,
    PubSelf,
    PubSuper,
    InPath(Path),
}

}
Syntax
PatternNoTopAlt:
    | name=IDENTIFIER => Pattern::Identifier(name)
    | _ => Pattern::Wildcard

PatternNoTopAlt name IDENTIFIER _
#![allow(unused)]
fn main() {
pub enum Pattern {
    Identifier(Identifier),
    Wildcard,
}

}

Submodules

#![allow(unused)]
fn main() {
#[path = "expressions.md.rs"]
pub mod expressions;
#[path = "items.md.rs"]
pub mod items;
#[path = "lexing.md.rs"]
pub mod lexing;
#[path = "print.md.rs"]
pub mod print;
#[path = "statements.md.rs"]
pub mod statements;
#[path = "types.md.rs"]
pub mod types;
#[path = "visitor.md.rs"]
pub mod visitor;

pub use expressions::*;
pub use items::*;
pub use lexing::*;
pub use print::*;
pub use statements::*;
pub use types::*;
pub use visitor::*;
}