New challenger joins Rust to topple C language

Zig interfaces closely with existing C code, but helps avoid common programming problems and security hazards

New challenger joins Rust to topple C language
Pixabay (Creative Commons BY or BY-SA)

Conventional wisdom has it that the C/C++ family of languages are a trade-off. All its power and speed comes at the cost of being a potential breeding ground for instabilities and security hazards. (Heartbleed and Shellshock, anyone?)

Option No. 1 is to fix the way that C/C++ apps are written, using better tooling or adding safety syntax. Option No. 2 is to ditch C entirely in favor of a new language -- Rust, for instance, or the newly proposed Zig.

Zig, from developer Andrew Kelley, is intended as a language for systems development "which prioritizes optimality, safety, and readability." It keeps some connections with the existing culture of C software development, to allow the graceful migration of existing C software, but its ultimate goal is to supplant C.

"I am nothing if not ambitious," Kelley says in his introduction to the language.

One of the key changes outlined in Zig is the "maybe type," a variable with optional null typing. By default, variables cannot be assigned null values, meaning that one of the most common bugaboos that plagues C/C++ programmers is avoided at the compiler level.

The preprocessor is another aspect of C that inspires a love/hate relationship. Directives to the C preprocessor have to be supplied in a syntax separate from the code itself. In Zig, however, preprocessor directives are part of the language's native syntax, so it's less difficult to reason about the generated code. "It's one language, not two," says Kelley.

Zig can interface directly with existing C code in both directions. Header files from C projects can be parsed in Zig, and Zig can be used to generate libraries with C ABIs. "You could write part of your application in C and part in Zig, link all the .o [or. DLL] files together and everything plays nicely with each other," Kelley writes.

Because the language is still in a very early, prototype stage, it's missing features -- like generics and macros -- found in more heavily developed counterparts like Rust and D. Both features are planned but not yet implemented in Zig.

Currently Rust stands as one of the most aggressively developed and better-known languages aimed specifically at systems and native applications. A few of Zig's ideas seem inspired by Rust, such as avoiding null types. But Zig is aimed slightly to the left of Rust, where people are more inclined to wean themselves off C piece by piece rather than all at once. This goes beyond Zig's syntax adhering more closely to C; it's also in Zig's memory management model, which is highly reminiscent of C. Rust aggressively promotes a different model for handling memory, which requires users to learn new metaphors. Zig still allows for unsafe behaviors, but makes it relatively easy to adopt safer ones.

Rust's biggest advantage right now is in momentum and brand-name recognition. In addition to springing from Mozilla, Rust is building up a vast library of third-party libraries (aka crates) and is starting to appear in actual shipping software (mainly Firefox). Zig is still a prototype with very little to call its own beyond a minimal standard library, but it's already possible to build running software with the language -- such as a simple Tetris clone.

Copyright © 2016 IDG Communications, Inc.