Rust in the Kernel vs Rust in User Space: What Changes
What changes when you write Rust for the Linux 6.18 kernel: no std, fallible allocation, errno Results, panics that act like BUG, and no main function.

If you already write Rust for the command line or a server, you know the language but not the kernel's version of it. The syntax is the same, and most of the habits you have built around Vec, String, println! and unwrap() are wrong inside a kernel module. This post lists the differences that matter, with code for each, using the Linux 6.18 sources: what is missing, what replaces it, and why. It is a map for someone who can read Rust and is about to open samples/rust/rust_minimal.rs. For the background on why the kernel has Rust at all, see Rust for Linux explained.
The points come from the kernel's Rust general information and the documentation inside the kernel crate (rust/kernel/).
There is no standard library
The first and biggest change: kernel Rust is no_std. The documentation says the Rust support in the kernel can link only core, not std, and that crates for use in the kernel must opt in with the #![no_std] attribute. In practice you get the language itself, Option, Result, iterators, slices, core::fmt, and the traits in core. You do not get std::collections, std::fs, std::thread, std::io or println!, because those assume an operating system underneath, and you are the operating system.
Even the alloc crate, which provides Vec and Box to no_std programs, is not used as it is. The kernel provides its own versions in kernel::alloc, discussed next. And for I/O, files, threads and sockets, you use kernel abstractions, such as kernel::fs, kernel::workqueue and kernel::task, which wrap the C kernel's equivalents.
Allocation can fail, and you must say how
In user-space Rust, Vec::push and Box::new abort the process if the system is out of memory, so you almost never see an error. In the kernel, an allocation failure is a normal event that code must handle, and the allocation also needs a context: whether the caller may sleep, may use reclaim, or is in an atomic section. The C world encodes that in GFP flags, and the Rust API keeps them. Here is the allocation from the kernel's own minimal sample:
use kernel::prelude::*;
fn build() -> Result<KVec<i32>> {
let mut numbers = KVec::new();
numbers.push(72, GFP_KERNEL)?;
numbers.push(108, GFP_KERNEL)?;
numbers.push(200, GFP_KERNEL)?;
Ok(numbers)
}KVec is a growable vector that uses the kernel's kmalloc, KBox is the boxed pointer, and every operation that may allocate takes a Flags argument and returns a Result. GFP_KERNEL means the allocation may sleep, which is right in a context like module initialization and wrong inside an interrupt handler, where GFP_ATOMIC applies. The ? operator returns the error to the caller if the allocation fails, converting the allocation error into the kernel's Error type, which is an errno. A user-space habit of assuming push cannot fail will not compile here, because the signature forces you to see the Result.
Errors are errno values, wrapped in a type
User-space programs choose their own error types. The kernel has one convention, negative errno numbers, and the Rust side wraps it: kernel::error::Error holds an errno, the constants such as ENOMEM, EINVAL and ENOENT are in kernel::error::code and the prelude, and kernel::error::Result<T> is Result<T, Error>. The functions that call into C convert the C return convention with to_result, which turns a negative int into Err:
use kernel::prelude::*;
fn open_thing(flag: bool) -> Result {
if !flag {
return Err(EINVAL);
}
Ok(())
}A Rust function exposed as a C callback returns the errno number the C caller expects. Because the error type is only an errno, there is no anyhow-style error with a message; context goes to the log with pr_err! at the point of failure.
unwrap and panic are not what you think
In a user program, unwrap() on an Err prints a message and ends the process. In the kernel there is no process to end. The kernel crate defines the panic handler, and the source in rust/kernel/lib.rs shows that it ends in the C BUG() call, so a Rust panic kills the current task with an oops, and with panic_on_oops set, the machine. The documentation of Result in the crate says that using unwrap() or expect() is heavily discouraged in the kernel because a panic may destabilize the system or entirely break it, just like the C BUG() macro, and points to the C documentation on when BUG() is acceptable.
The same applies to arithmetic. CONFIG_RUST_OVERFLOW_CHECKS is enabled by default, and with it a Rust panic occurs on integer overflow, so a + b in a kernel module can panic where the C equivalent would silently wrap. Use checked_add, wrapping_add or saturating_add when overflow is a real possibility, and decide on purpose.
The practical rule: handle errors with ? and Result, return an errno, and reserve unwrap for the place a C programmer would have written BUG_ON.
There is no main, and printing goes to the log
A user program starts at main and ends when it returns. A kernel module has an entry point called when it is loaded and optionally an exit when it is removed, described with the module! macro and the kernel::Module trait:
use kernel::prelude::*;
module! {
type: Hello,
name: "hello",
authors: ["Example Author"],
description: "Hello module",
license: "GPL",
}
struct Hello;
impl kernel::Module for Hello {
fn init(_module: &'static ThisModule) -> Result<Self> {
pr_info!("hello from the kernel\n");
Ok(Hello)
}
}
impl Drop for Hello {
fn drop(&mut self) {
pr_info!("goodbye\n");
}
}The init function is the entry point, and its return value is the module's state. If it returns Err, loading fails with that errno. Cleanup is the Drop implementation: when the module is unloaded, the value is dropped and drop runs. That replaces the separate exit function of C modules and is the same ownership rule you already know. Output goes through pr_info!, pr_err! and the related macros, which write to the kernel log and take a format string with a trailing newline, as printk does. There is no stdout.
Everything is shared state
User-space Rust encourages passing data by value and owning it. A kernel module's state lives for as long as the module is loaded and is touched by many contexts: system calls on many CPUs, interrupt handlers, work items. The kernel crate therefore provides its own synchronization types, Mutex, SpinLock and Arc, which wrap the kernel's primitives. They have the lock-owns-data design you may know from std::sync::Mutex, but a few differences matter: a Mutex may sleep and so cannot be used where sleeping is forbidden, a SpinLock can be used in atomic contexts, and both must be pinned, initialized in place with the pin_init machinery rather than moved after creation, because the C structure inside holds pointers to itself.
Threads are not std::thread; you schedule work on a workqueue or create a kernel thread through the task APIs. There is no std::sync::mpsc, no async runtime and no Instant::now(): time comes from kernel time abstractions.
Resource limits are tighter
Kernel stacks are small, 16 KiB on x86_64, shared with every function in the call chain, so large arrays on the stack are a bug. Floating point is not available to kernel code in general, because the kernel does not save FPU state on entry from user space. There is no #[cfg(test)] harness in the user-space sense; tests run as KUnit tests inside the kernel, written with the kunit_tests macro, as the kernel testing document describes.
What stays the same
The language is the same: ownership, borrowing, traits, generics, pattern matching, Option and Result, iterators and the macro system all work as you expect. The compiler enforces the same rules, which is what makes the kernel crate possible. Most of the effort of moving from user space to kernel is learning the abstraction layer, not the language.
Where this comparison stops
The details above are for Linux 6.18. The API is moving: names such as KVec and KBox replaced earlier ones, and a function you find in an older tutorial may not exist. Also, some of the user-space habits discussed here, such as avoiding unwrap, are good practice anywhere. The kernel makes them rules.


