Files
carbon-lang/docs/images/snippets.md
T
gleb 4370acd1bb Snippets update (#6204)
~~Added explanatory comment about math package usage~~
Changed Main() entry point to Run() as per design and toolchain

This small update of front page code snippets will add
explanatory comment to highlight that provided Carbon code
is  hypothetical and meant to show the look and feel of the language.

Also it delivers change of Main() to Run() to
highlight correct entry point for Carbon lang.
2025-10-17 13:09:46 +00:00

2.6 KiB

Front page snippets for Carbon

Images

Images are managed in Google Drive.

Quicksort

A sample of quicksort in Carbon.

package Sorting;

fn Partition[T:! Comparable & Movable](s: Slice(T))
     -> i64 {
  var i: i64 = -1;

  for (e: T in s) {
    if (e <= s.Last()) {
      ++i;
      Swap(&s[i], &e);
    }
  }
  return i;
}

fn QuickSort[T:! Comparable & Movable](s: Slice(T)) {
  if (s.Size() <= 1) {
    return;
  }
  let p: i64 = Partition(s);
  QuickSort(s[:p - 1]);
  QuickSort(s[p + 1:]);
}

Carbon and C++

C++

// C++:
#include <numbers>
#include <print>
#include <span>
#include <stdfloat>
#include <vector>
// or: import std;

struct Circle {
  std::float32_t r;
};

void PrintTotalArea(std::span<Circle> circles) {
  std::float32_t area = 0;
  for (const Circle& c : circles) {
    area += std::numbers::pi * c.r * c.r;
  }
  std::print("Total area: {}\n", area);
}

auto main() -> int {
  std::vector<Circle> circles = {{.r = 1.0}, {.r = 2.0}};
  // Implicitly converts `vector` to `span`.
  PrintTotalArea(circles);
  return 0;
}

Carbon

// Carbon:
package Geometry;
import Math;

class Circle {
  var r: f32;
}

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

fn Run() -> i32 {
  // A dynamically sized array, like `std::vector`.
  var circles: array [Circle] = ({.r = 1.0}, {.r = 2.0});
  // Implicitly constructs a slice from the array.
  PrintTotalArea(circles);
  return 0;
}

Mixed

// C++ code used in both Carbon and C++:
#include <stdfloat>

struct Circle {
  std::float32_t r;
};

// Carbon exposing a function for C++:
package Geometry;
import Cpp library "circle.h";
import Math;

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

// C++ calling Carbon:
#include <vector>
#include "circle.h"
#include "geometry.carbon.h"

auto main() -> int {
  std::vector<Circle> circles = {{1.0}, {2.0}};
  // A Carbon slice supports implicit construction
  // from `std::vector`, similar to `std::span`.
  Geometry::PrintTotalArea(circles);
  return 0;
}