Menu
CoddyTech
Course

Rust for C Developers

Move from C structs, pointers, and manual storage to Rust ownership, borrowing, enums, and traits. Practice 48 multi-file tasks, build a parcel depot, and port two C reference programs.

C and Rust side by side

The same program in C and in Rust, from the lessons of this course.

  1. Output and Comments

    C starts in main and calls printf or puts; Rust starts in fn main() and uses macros such as println!. The exclamation mark is part of the macro name. Both languages accept // line comments and /* ... */ block comments. A comment removes source text from…

    C
    1#include <stdio.h>2int main(void) {3    // printf("ignored\n");4    printf("port=%d\n", 7);5    puts("// data");6    return 0;7}
    Rust
    1fn main() {2    // println!("ignored");3    println!("port={}", 7);4    println!("// data");5}
  2. Boolean Conditions

    C conditions accept scalar values: zero is false and nonzero is true. Rust requires a bool condition; an integer does not implicitly become a boolean. Write an explicit comparison such as count != 0. The operators &&, ||, and ! mean logical and, or, and not…

    C
    1#include <stdio.h>2#include <stdbool.h>3int main(void) {4    int divisor = 0;5    bool usable = divisor != 0 && 12 / divisor > 2;6    printf("%d %d\n", usable, !usable);7    return 0;8}
    Rust
    1fn main() {2    let divisor: i32 = 0;3    let usable = divisor != 0 && 12 / divisor > 2;4    println!("{} {}", usable, !usable);5}
  3. Functions and Returns

    A Rust function declares each parameter type and places its result type after ->. fn label(n: i32) -> &'static str receives an integer and returns a borrowed string literal. String literals live for the entire program, which is what the supplied 'static…

    C
    1#include <stdio.h>2const char *label(int value) {3    if (value == 0) return "idle";4    return value % 2 == 0 ? "even" : "odd";5}6int main(void) {7    printf("%s %s\n", label(0), label(7));8    return 0;9}
    Rust
    1fn label(value: i32) -> &'static str {2    if value == 0 { return "idle"; }3    if value % 2 == 0 { "even" } else { "odd" }4}5fn main() {6    println!("{} {}", label(0), label(7));7}
  4. Ownership and Moves

    C assignment copies a pointer, so two pointer variables may refer to one allocation; deciding which one frees it is your responsibility. Rust String owns an allocation. Assigning it to another binding moves ownership and makes the old binding unusable.…

    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4int main(void) {5    char *first = malloc(5);6    if (!first) return 1;7    strcpy(first, "note");8    char *copy = malloc(5);9    if (!copy) { free(first); return 1; }10    strcpy(copy, first);11    char *owner = first;12    first = NULL;13    owner[0] = 'N';14    int a = 3, b = a;15    printf("%s %s %d %d\n", owner, copy, a, b);16    free(owner);17    free(copy);18    return 0;19}
    Rust
    1fn main() {2    let first = String::from("note");3    let copy = first.clone();4    let mut owner = first;5    owner.replace_range(0..1, "N");6    let a = 3;7    let b = a;8    println!("{} {} {} {}", owner, copy, a, b);9    drop(owner);10}
  5. Strings and UTF-8

    Rust String owns valid UTF-8; &str borrows valid UTF-8. Its len() counts bytes, as strlen does for a C UTF-8 byte string, but Rust has no terminating zero byte. A char is a Unicode scalar value. Use chars() to visit scalars and char_indices() to obtain their…

    C
    1#include <stdio.h>2#include <string.h>3int main(void) {4    unsigned char word[5] = {0xc3, 0xa9, 'x', 0, 0};5    int continuation = (word[1] & 0xc0) == 0x80;6    strcat((char *)word, "!");7    printf("%s %s\n", continuation ? "invalid" : "valid", (const char *)word + 2);8    return 0;9}
    Rust
    1fn main() {2    let mut word = String::from("éx");3    let invalid = word.get(1..).is_none();4    word.push('!');5    let suffix = word.get(2..).unwrap();6    println!("{} {}", if invalid { "invalid" } else { "valid" }, suffix);7}
  6. Option and Matching

    C APIs often encode absence using a null pointer, a sentinel index or a boolean plus an output parameter. Rust represents it directly with Option<T>: either Some(value) or None. The type forces callers to consider that a value might not exist. A match can…

    C
    1#include <stdio.h>2#include <stddef.h>3int find_zero(const int *values, size_t n, size_t *out) {4    for (size_t i = 0; i < n; ++i) if (values[i] == 0) { *out = i; return 1; }5    return 0;6}7int main(void) {8    int values[] = {0, 5};9    size_t index = 99;10    if (find_zero(values, 2, &index)) printf("found:%zu\n", index);11    else puts("missing");12    return 0;13}
    Rust
    1fn main() {2    let values = [0, 5];3    match values.iter().position(|&n| n == 0) {4        Some(index) => println!("found:{}", index),5        None => println!("missing"),6    }7}

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

More courses for C developers

Learn Rust from another language

Frequently asked questions

Who is Rust for C Developers for?

For people who already write C and want to learn Rust. Each lesson starts from C you know and shows how Rust does the same thing, so you skip the beginner basics.

Do I need to know Rust already?

No. The course starts with your first Rust program. You need C basics: variables, conditions, loops, functions and collections.

How many lessons does Rust for C Developers have?

Rust for C Developers contains 48 interactive lessons you can complete at your own pace.

What does Rust for C Developers cover?

The course is organized into chapters covering: First Rust Programs, Decisions and Loops, Functions and Sequences, Ownership and Borrowing, and more.

Is Rust for C Developers free?

Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.

Coddy programming languages illustration

Start Rust for C Developers for free

Get started