C for Rust Programmers
Recorded: Oct. 10, 2026, 12:11 p.m.
| Original | Summarized |
C for Rust Programmers Boolean Is Not* Built-In Null-Terminated Strings Target-Dependent Integer Widths Error Handling Sucks Field Access Syntax Arrays Become Pointers for Fun Oh Yeah References Don't Exist Conclusion ← Return C for Rust Programmers 2026-10-07 #rust#c The very first systems programming language I ever learned was Rust. This is uncommon compared to many other programmers; you're more likely to find someone who learned C or C++ first before coming to Rust. As such, there are plenty of "Rust for C Programmers" articles on the internet, but little to no "C for Rust Programmers" articles out there. The original version of C did not have a primitive type for booleans, programs instead used the integers 0 and 1. This was changed in C99, which added booleans in the optional <stdbool.h> header[1]: int main() { return 0; A Rust &str is 16 bytes: 8 bytes for the memory address and 8 bytes for the string length. This is because str is a dynamically sized type, and uses pointer metadata to track the length of the string. // ...strings use 16 bytes. C programs don't need to keep track of an extra size variable alongside the string[3] This means empty strings "" still take up one byte of memory! Null bytes cannot easily be used in the middle of a string without messing up <string.h>'s functions In practice, this requires you to remember to allocate extra space for the null terminator and insert it at the end of strings. For example, here is a program that reverses a string in C: for i in 0..len { unsafe { reversed.assume_init() } C's integer types are not guaranteed to use an exact number of bits, instead the width varies depending on the target platform. TypeC Standard64-bit Unix64-bit Windows Data sourced via cppreference.com long being 64 bits on Unix and 32 bits on Windows particularly irks me. I recommend following the advice that a friend of mine gave me a few years ago: if you care about cross-platform compatibility, only use the fixed width integers provided by <stdint.h>: Error Handling Sucks I really love Rust's error handling. Result forces you to address errors, and sum types (enums) and match statements make doing so really easy! for (int i = 0; i < len; i++) { if (reversed == NULL) { C has two different field access operators: . for values and -> for pointers. struct Foo value = { 103 }; // Accessing the field directly. // Accessing the field through a pointer. If your curious, this Stack Overflow write up provides some interesting history on why the -> operator exists.[7] Arrays have some weird nuance to them. Sometimes they are plain values with their length accessible using sizeof(), other times they are pointers with an unknown size. In general, this is determined by whether you are handling the array within the function it was defined or not. printf("Declared size (main): %ld bytes\n", sizeof(declared_size)); I'd be remiss if I didn't mention it once. C has no borrow checking, and no concept of references. It's all raw pointers. This means you can do fun pointer arithmetic like so: // Woah, iterating over pointers instead of indexes! Vulnerability category distribution for CVEs registered between Nov. 2021 and Jan. 2022, via I hope you enjoyed this article! I've honestly had a lot of fun learning C. And while I doubt I'll reach for it for personal projects, it's absolutely a crucial language to know as a systems programmer. All the examples in this blog are available on Github, if you'd like to mess with them yourself! Technically you can use _Bool without the header, but you still need it for the true and false definitions. ↩ It looks like Clang still has only partial support for C23, so you may need to wait a little while longer before chucking away your #include <stdbool.h> statements. ↩ As described previously, this is essentially what Rust does. In C this would be an ergonomic pain, but Rust's language features make it so a programmer never needs to consider the pointer and the string length as separate variables. ↩ The corresponding Rust function is not idiomatic, and can only correctly handle ASCII text. If I were writing a production version of this function, it would be a single line of code: forward.chars().rev().collect::<String>(). ↩ usize and isize don't perfectly map semantically to size_t and ptrdiff_t. I don't fully understand the difference, however, so I recommend doing your own research before using these types. ↩ ↩2 This blog post on Fearless SIMD provides a great example of using Rust's type system to guarantee the correctness of code. I highly recommend giving it a read! ↩ My favorite line of code from that post is 100->a = 0;, it's so cursed! ↩ ©2026 BD103 RSS https://bd103.dev/feed.xml |
The author reflects on several structural and philosophical differences between the C programming language and the paradigm exemplified by Rust, detailing concepts that often feel counter-intuitive to programmers accustomed to Rust's guarantees. One notable divergence is in boolean handling. The original C language did not possess a built-in boolean type, relying instead on the integers 0 and 1. While C99 introduced the <stdbool.h> header to provide boolean types, true and false were initially handled through macros defining 1 and 0. Though C23 made booleans language primitives, the necessity of including headers or relying on external definitions still highlights a difference from statically typed, integrated systems found elsewhere. String management in C contrasts sharply with the approach seen in Rust. C uses null-terminated strings, where every string must end with a null byte (\0). This design choice sacrifices memory efficiency, as empty strings still occupy one byte, and it necessitates manual bookkeeping for the null terminator, which increases the risk of errors like out-of-bounds reads if the terminator is overlooked. Conversely, Rust's approach, as noted by the author, integrates pointer and string length management, aiming to eliminate the need for programmers to explicitly manage these details. Integer width in C is platform-dependent, a feature that causes impedance in cross-platform development. Integer types like long and long long do not have guaranteed bit widths across different architectures (e.g., 32 bits on Windows versus 64 bits on Unix). The author strongly advocates for using fixed-width integer types provided in <stdint.h>, such as uint8_t and int64_t, to ensure portable and predictable memory handling, a practice fundamentally contrasted with Rust's strong type system. Error handling in C is described as deficient compared to modern practices. C delegates the responsibility of error management to the programmer, typically relying on functions returning specific integer error codes or null pointers, often requiring additional checks against system variables like errno. This approach lacks the guarantees provided by Rust’s result-based error handling, where the compiler forces the programmer to address potential failure states through techniques like sum types and match expressions, leading to safer program composition. The author critiques C’s trust placed entirely on the programmer to manage memory allocation (like malloc), noting that failure to check return values can lead to severe issues like segmentation faults. The syntax for accessing data also differs significantly. C employs separate operators for accessing values directly using the dot operator and accessing fields through a pointer using the arrow operator. This is distinct from Rust’s unified use of the dot operator for all field access. Furthermore, the treatment of arrays presents a complex issue regarding memory layout. Arrays can behave as plain values with static sizes or as pointers when passed to functions, where they implicitly decay into pointers to their first element. This behavior can lead to confusing results when using the sizeof operator in function parameters, as the size reported is that of a pointer rather than the full array data. This complexity is further exacerbated by the lack of concepts like borrow checking and references in C, which means the language permits raw pointer arithmetic that can easily lead to buffer overflows and security vulnerabilities if not meticulously managed by the developer. In conclusion, the author posits that while learning C is valuable for understanding systems programming, the language presents significant ergonomic and safety trade-offs compared to Rust. The fundamental difference lies in C’s reliance on manual memory management and explicit error handling, which shifts the burden of correctness onto the programmer, whereas Rust uses its type system to enforce safety and correctness at compile time. |