Menu
CoddyTech
Course

Rust for R Developers

Build on R vectors, lists, closures and environments to learn Rust ownership, borrowing, enums and traits. Complete 48 multi-file tasks, build a parcel depot and port two R programs.

R and Rust side by side

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

  1. Output and Comments

    R evaluates top-level expressions and uses a hash for comments, while a Rust executable begins at fn main and uses slash comments. A marker inside quoted text remains data. R cat writes exactly the supplied text without automatically appending a newline; the…

    R
    1# This line is ignored.2cat("level:")3cat(sprintf("%d # literal\n", 2 + 3))
    Rust
    1fn main() {2    // This line is ignored.3    print!("level:");4    println!("{} # literal", 2 + 3);5}
  2. Boolean Conditions

    R if requires a single nonmissing condition; an empty vector is not a convenient falsey container, and NA cannot decide a branch. Test length explicitly for emptiness as the source does. Rust requires bool rather than coercing a numeric length into a…

    R
    1items <- numeric()2present <- length(items) > 0L3chosen <- if (present) "present" else "fallback"4cat(chosen, "\n", tolower(as.character(present)), "\n", sep = "")
    Rust
    1fn main() {2    let items: Vec<i32> = Vec::new();3    let present = !items.is_empty();4    println!("{}", present);5    println!("{}", present && items[0] == 9);6}
  3. Functions and Returns

    R functions return their final evaluated expression unless return exits explicitly. Arguments are generally promises evaluated when used, and parameter types are not declared in these ordinary functions. Rust signatures specify parameter and result types, and…

    R
    1differs <- function(left, right) left != right2cat(tolower(as.character(differs("a", "b"))), "\n", sep = "")3cat(tolower(as.character(differs("a", "a"))), "\n", sep = "")
    Rust
    1fn differs(left: char, right: char) -> bool { left != right }2fn main() {3    println!("{}", differs('a', 'b'));4    println!("{}", differs('a', 'a'));5}
  4. Ownership and Moves

    R ordinary vectors have value semantics, so changing the second binding does not mutate the vector value observed through first. Implementations may share storage until modification, but that is not Rust move semantics. R environments are reference-like and…

    R
    1first <- c(6, 8)2second <- first3second <- rev(second)4cat(first[1], "\n", second[1], "\n", sep = "")
    Rust
    1fn main() {2    let first = vec![6, 8];3    let mut second = first;4    second.reverse();5    let mut independent = second.clone();6    independent.clear();7    println!("{}", second[0]);8    println!("{}", independent.len());9}
  5. Strings and UTF-8

    R character operations depend on text encoding and operation choice. This source explicitly marks UTF-8, uses substr for its first character, and charToRaw for encoded bytes. Rust String and str guarantee valid UTF-8, chars yields Unicode scalar values, and…

    R
    1word <- enc2utf8("éx")2cat(substr(word, 1L, 1L), "\n", sep = "")3cat(as.integer(charToRaw(word)[1L]), "\n", sep = "")
    Rust
    1fn main() {2    let word = "éx";3    println!("{}", word.chars().next().unwrap());4    println!("{}", word.get(0..1).is_none());5}
  6. Option and Matching

    R NULL and NA express different kinds of missingness: NULL is a zero-length object, while NA can be an unknown element inside a typed vector. A positive out-of-range vector index can yield NA, so this source guards its access and deliberately returns NULL for…

    R
    1values <- c(0)2first <- if (length(values) >= 1L) values[1L] else NULL3missing <- if (length(values) >= 3L) values[3L] else NULL4cat(tolower(as.character(is.null(first))), "\n", sep = "")5cat(tolower(as.character(is.null(missing))), "\n", sep = "")
    Rust
    1fn main() {2    let values = vec![0];3    println!("{}", values.get(0).is_none());4    println!("{}", values.get(2).is_none());5}

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

More courses for R developers

Learn Rust from another language

Frequently asked questions

Who is Rust for R Developers for?

For people who already write R and want to learn Rust. Each lesson starts from R 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 R basics: variables, conditions, loops, functions and collections.

How many lessons does Rust for R Developers have?

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

What does Rust for R 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 R 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 R Developers for free

Get started