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

Items

This chapter specifies the top-level items that can appear in a Dada source file.

Source Files

A Dada source file defines a module. The module name is derived from the file name. A source file contains zero or more items, optionally followed by zero or more statements:


SourceFile ::= Item* Statement*

If a source file contains top-level statements, they are wrapped in an implicit async fn main() function.

An item Item is one of the following:


Item ::= Function
         | Class
         | Struct
         | UseDeclaration

Visibility definition

Items and fields may have a visibility modifier. Without a modifier, the item is private to the enclosing module.


Visibility ::= pub
               | export
               | ε

Function definition

A function Function is declared with the fn keyword, optionally preceded by effect keywords and followed by a name, optional generic parameters, parameters, optional return type, optional where clause, and a body or semicolon:


Function ::= Visibility Effect* fn Identifier GenericParameters?
             ( Parameters ) ReturnType? WhereClause? FunctionBody

Effect definition

Effect keywords may appear in any order before fn:


Effect ::= async
           | unsafe

Parameters definition

Function parameters are enclosed in parentheses and separated by commas:


Parameters ::= FunctionInput,*
FunctionInput ::= SelfParameter | Parameter

A function may have a self parameter as its first parameter, optionally preceded by a permission keyword, which makes it a method:


SelfParameter ::= PermissionKeyword? self

Each non-self parameter has the form name: Type. A parameter may be preceded by mut to declare a mutable binding:


Parameter ::= mut? Identifier : Type

FunctionBody definition

A function may have a body, which is a block enclosed in curly braces. If no body is present, the function has no definition.


FunctionBody ::= Block | ε

ReturnType definition

A function may declare a return type with -> followed by a Type after the parameters.

ReturnType ::= -> Type

GenericParameters definition

A function may declare generic parameters in square brackets after the name:


GenericParameters ::= [ GenericParameter,* ]
GenericParameter ::= type Identifier | perm Identifier

WhereClause definition

A function may have a where clause after the return type that constrains its generic parameters:


WhereClause ::= where WhereConstraint,+
WhereConstraint ::= Type is WhereKind (+ WhereKind)*
WhereKind ::= ref
              | mut
              | shared
              | unique
              | owned
              | lent

Class definition

A class Class is declared with the class keyword. Classes have reference semantics.


Class ::= Visibility class Identifier GenericParameters?
          ConstructorFields? WhereClause? ClassBody?

ConstructorFields definition

A class may declare constructor fields in parentheses after the name:


ConstructorFields ::= ( Field,* )

ClassBody definition

A class body enclosed in curly braces may contain field declarations and method definitions:


ClassBody ::= { ClassMember* }
ClassMember ::= Field
                | Method

Method definition

A method Method is a function declared inside a class or struct body:


Method ::= Function

Field definition

A field declaration Field has the form:


Field ::= Visibility mut? Identifier : Type

Generics and Where Clauses

Classes support generic parameters and where clauses with the same syntax as functions.

Struct definition

A struct Struct is declared with the struct keyword. The syntax is identical to Class but structs have value semantics.


Struct ::= Visibility struct Identifier GenericParameters?
           ConstructorFields? WhereClause? ClassBody?

UseDeclaration definition

A use declaration UseDeclaration imports a name from another crate, optionally renaming it with as:


UseDeclaration ::= use Path (as Identifier)?
Path ::= Identifier (. Identifier)*