Carbon, or Carbon-Lang, is an experimental, general-purpose programming language.
The project is open-source and was started by Google, following in the footsteps of previous Google-made programming languages (Go and Dart). Google engineer Chandler Carruth first introduced Carbon at the CppNorth conference in Toronto in July 2022. He stated that Carbon was created to be a C++ successor.
The language is expected for a 1.0 release to occur in 2024 or 2025. The language intends to fix several perceived shortcomings of C++ but otherwise provides a similar feature set.
The main goals of the language are readability and “bi-directional interoperability”, as opposed to using a new language like Rust (which, while being based on C++, is not two-way compatible with C++ programs). Changes to the language will be decided by the Carbon leads.
Carbon’s documents, design, implementation, and related tools are hosted on GitHub under the Apache-2.0 license with LLVM Exception.
In this article we will see how to install Carbon in Ubuntu and the first steps.
sudo apt update
sudo apt install sudo apt install build-essential curl git zlib1g-dev
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
brew install gcc python@3.9 bazelisk llvm
llvm
to the $PATH
variable:pip3.9 install -U pip
export PATH="$(brew --prefix llvm)/bin:${PATH}"
git clone https://github.com/carbon-language/carbon-lang
cd carbon-lang
bazel run //explorer -- ./explorer/testdata/print/format_only.carbon
vim ./explorer/testdata/prefix/sandbox.carbon
package sample api;
fn Main() -> i32 {
Print("Hello, World!");
return 0;
}
To compile, run:
bazel run //explorer -- ./explorer/testdata/print/sandbox.carbon
package sample api;
fn Main() -> i32 {
var s: auto = "Hello, Carbon";
Print(s);
return 0;
}
if..else
conditionspackage sample api;
fn Main() -> i32 {
let x: i32 = 42;
if( x == 6 ){
Print("x is not equal to 6!");
}else{
Print("x is equal to {0}", x);
}
return 0;
}
package sample api;
fn helloWorld() -> String {
return "Hello world via function!";
}
fn Main() -> i32 {
Print( helloWorld() );
return 0;
}
package sample api;
fn mySum(var x: i32, var y: i32) -> i32 {
return x + y;
}
fn Main() -> i32 {
Print("The sum of x + y: {0}", mySum(9, 3) );
return 0;
}
For more information see the examples in the explorer/testdata directory and the documentation.
If you want, try using Carbon online at: https://carbon.godbolt.org/.