Sage-Code Laboratory
index<--

Carbon Syntax

The Carbon language was introduced by Google as a next generation programming language with the goal to be C++'s successor. One of Carbon's other goals is to bring the improved developer experience seen in other modern languages.

Syntax Rules

Example:
//Program entry point
fn Main() -> i32 {
  Print("Hello World");
  return 0;
}

Notes:

In this example "fn" is a keyword that define a function.

Basic Concepts

There is no better time than now to introduce you the basic concepts and notations of Carbon syntax. We will show you an example then we will make notes to explain the concepts. We explain them as simple as possible so you can learn Carbon as your first programming language.

Comments

Code Block

Carbon has a concept of code block. It is a compact region of code separated by squiggly brackets: {....}. In other languages we start a block using "begin" and terminate with "end" keywords.

Indentation

In Carbon the indentation is not relevant to the code but it is recommended. What is relevant is that we can have nested blocks of code. The outer block can contain one or more inner blocks. Each block of code define a local scope.

Variables

The variable declaration in Carbon is done using keyword "var" and symbol "=". Once a variable is initialized we call this "binding". So a variable is bound to it's value. A variable that is not bound can't be used. To declare a type for a variable we use symbol ":" after the variable name.

A variable is an identifier or a name that can start with "_" or any alphabetic character but not a special character or a number. So this is almost like any other language.

Examples:

/* define variables */
var x: i32 = 5;
var f: f32 = 0.1; 
var str: String = "Hello";
Work In Progress

Read next: Data Types