11.10.20223 min
Maciej Olanicki

Maciej OlanickiRedakcja Bulldogjob

Carbon - Google's new programming language instead of Rust

Rust as the follower to C and C++ in low-level code has received much recognition and interest, yet Google is working on something new. Why?

Carbon - Google's new programming language instead of Rust

Everyone has probably already seen this XKCD comic. The one in which the will to build something new, to replace the current standard with a better one, always results in the establishment of a new standard that, instead of replacing the previous one, occurs alongside it. A good, but not entirely isolated example would be JavaScript frameworks, the number of which seems to grow exponentially every year.

Rust instead of C..

The joy of creation, however, seems to be much more widespread, as reported by Google spokesmen. Indeed, a new programming language has been created within the corporation's bosom. And it probably wouldn't have caught the attention of many if it weren't for the fact that the language is intended to perform the same functions as another, quite modern and applicable wherever other languages have functioned (and often failed) for years, Rust. 

This may come as a surprise. After all, the whole industry is putting up high expectations for Rust, in a moment (probably next Monday) the first piece of code written in Rust will go into the Linux kernel. In addition, Microsoft specialists have just diagnosed the lion's share of security problems in the chosen programming language - the C/C++ duet, which has been used for 40 years, is said to lead to memory leaks. Rust is supposed to be the remedy. 

Rust, which is a modern, low-level programming language that does not duplicate the flaws of its predecessors. And yet it fails to meet some of the expectations, as can be seen from the decisions made at Google. Indeed, it was announced in Mountain View that a new low-level programming language, Carbon, will be developed. The project is currently in the experimental phase, but has far-reaching ambitions. 

...Carbon instead of Rust?

Carbon's specifications are expected to be published before the end of the year. The principium in designing the language was memory safety, which, after all, Rust is also famous for. This raises questions – why does Google need Carbon? Well, Carbon is not meant to replace Rust as the follower to C/C++ in low-level code, but to appear alongside it. And more precisely, wherever there is a large code base in C/C++.

Example code in C++:

#include <math.h>
#include <iostream>
#include <span>
#include <vector>

struct Circle {
  float r;
};

void PrintTotalArea(std::span<Circle> circles) {
  float area = 0;
  for (const Circle& c : circles) {
    area += M_PI * c.r * c.r;
  }
  std::cout << "Total area: " << area << "\n";
}

auto main(int argc, char** argv) -> int {
  std::vector<Circle> circles = {{1.0}, {2.0}};
  PrintTotalArea(circles);
  return 0;
}


...and the equivalent in Carbon:

package Geometry api;
import Math;

class Circle {
  var r: f32;
}

fn PrintTotalArea(circles: Slice(Circle)) {
  var area: f32 = 0;
  for (c: Circle in circles) {
    area += Math.Pi * c.r * c.r;
  }
  Print("Total area: {0}", area);
}

fn Main() -> i32 {
  var circles: Array(Circle) = ({.r = 1.0}, {.r = 2.0});
  PrintTotalArea(circles);
  return 0;
}


Carbon is thus an intermediate language, conceived in such a way as to guarantee the greatest possible interoperability with C and C++, and at the same time Rust-like memory safety. By Google employees themselves, this is compared to the decisions made by Apple on Objective-C. So we are talking about an initiative that is more of a bridge than strictly a novelty. More about Carbon documentation on GitHub.

<p>Loading...</p>