Why Rust Felt So Hard Until I Unlearned These 5 Java Habits
Java builds habits. After enough years with it, certain patterns become automatic. You reach for them without thinking. Most of the time that is fine. In Rust, several of those habits work directly against you.
This is not about Java being bad. It is about the mental model being different enough that the habits you built actually slow you down when you switch. These are the five that cost me the most time.
1. Expecting null to be a valid state
In Java, any reference type can be null. You learn early to check for it:
String name = getName();
if (name != null) {
System.out.println(name.toUpperCase());
}
String name = getName();
if (name != null) {
System.out.println(name.toUpperCase());
}
You get used to null being everywhere. It is the default absence value. You forget it is there until a NullPointerException reminds you at runtime.
Rust has no null. There is no way to have a reference that points to nothing. If a value might be absent, the type itself has to say so:
Option {
Some(String::from("Zeeshan"))
// or None
}
let name = get_name();
match name {
Some(n) => println!("{}", n.to_uppercase()),
None => println!("no name"),
}” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
fn get_name() -> Option<String> {
Some(String::from("Zeeshan"))
// or None
}
let name = get_name();
match name {
Some(n) => println!("{}", n.to_uppercase()),
None => println!("no name"),
}
fn get_name() -> Option<String> {
Some(String::from("Zeeshan"))
// or None
}
let name = get_name();
match name {
Some(n) => println!("{}", n.to_uppercase()),
None => println!("no name"),
}
Option<T> is an enum with two variants. Some(value) means there is a value. None means there is not. The compiler forces you to handle both cases. You cannot call methods on an Option<String> as if it were a String. The types are different.
The habit to break: reaching for null as a valid return value. In Rust, if something might not be there, the return type is Option. If it is always there, the return type is plain T. The type signature tells you everything upfront.
2. Mutating objects freely
Java objects are mutable by default. You pass an object to a method and that method can change it. You do this constantly without thinking:
<button data-code="List names = new ArrayList();
names.add("Alice");
addMore(names); // addMore can mutate names freely” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-1/2 -translate-y-1/2 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
List<String> names = new ArrayList<>();
names.add("Alice");
addMore(names); // addMore can mutate names freely
List<String> names = new ArrayList<>();
names.add("Alice");
addMore(names); // addMore can mutate names freely
In Rust, everything is immutable by default. You have to explicitly opt into mutation:
let names = vec!["Alice"]; // immutable
let mut names = vec!["Alice"]; // mutable
names.push("Bob");
let names = vec!["Alice"]; // immutable
let mut names = vec!["Alice"]; // mutable
names.push("Bob");
That is the easy part. The harder part is what happens when you pass something mutable to a function.
In Rust there is one rule: you can have either one mutable reference to something, or any number of immutable references. Never both at the same time.
<button data-code="fn add_name(names: &mut Vec, name: String) {
names.push(name);
}
let mut names = vec![String::from("Alice")];
add_name(&mut names, String::from("Bob"));” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
fn add_name(names: &mut Vec<String>, name: String) {
names.push(name);
}
let mut names = vec![String::from("Alice")];
add_name(&mut names, String::from("Bob"));
fn add_name(names: &mut Vec<String>, name: String) {
names.push(name);
}
let mut names = vec![String::from("Alice")];
add_name(&mut names, String::from("Bob"));
The &mut is explicit both at the call site and in the function signature. Anyone reading this code knows immediately that add_name modifies the list. In Java that information is buried in the method implementation.
The habit to break: assuming you can mutate anything you have a reference to. In Rust, mutability is opt-in and the borrow checker enforces that you never have conflicting access.
3. Using exceptions for control flow
Java has checked and unchecked exceptions. Many Java codebases use exceptions not just for genuine failures but as a way to exit a method early or signal a condition:
public User findUser(int id) throws UserNotFoundException {
User user = db.find(id);
if (user == null) {
throw new UserNotFoundException(id);
}
return user;
}
public User findUser(int id) throws UserNotFoundException {
User user = db.find(id);
if (user == null) {
throw new UserNotFoundException(id);
}
return user;
}
The exception jumps up the call stack until something catches it. It is invisible in the type signature unless it is a checked exception, and even then many developers just add throws Exception and move on.
Rust has no exceptions. A function that can fail says so in its return type:
Result {
match db.find(id) {
Some(user) => Ok(user),
None => Err(UserError::NotFound(id)),
}
}” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
fn find_user(id: u32) -> Result<User, UserError> {
match db.find(id) {
Some(user) => Ok(user),
None => Err(UserError::NotFound(id)),
}
}
fn find_user(id: u32) -> Result<User, UserError> {
match db.find(id) {
Some(user) => Ok(user),
None => Err(UserError::NotFound(id)),
}
}
The caller cannot ignore this. They get a Result back and they have to decide what to do with it. The ? operator makes propagating errors upward concise:
Result {
let user = find_user(id)?;
Ok(Profile::from(user))
}” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
fn load_profile(id: u32) -> Result<Profile, UserError> {
let user = find_user(id)?;
Ok(Profile::from(user))
}
fn load_profile(id: u32) -> Result<Profile, UserError> {
let user = find_user(id)?;
Ok(Profile::from(user))
}
If find_user returns an Err, the ? returns it immediately from load_profile. Same behavior as throwing an exception, but it is explicit in every type signature along the way.
The habit to break: treating errors as things that happen out of band. In Rust, errors are values. They live in the return type. You pass them around, transform them, and handle them like any other data.
4. Thinking classes own behavior
In Java, a class bundles data and behavior together. You define fields and methods in one place. Inheritance lets you share behavior across types. This is so fundamental to Java that it shapes how you think about designing anything:
class Animal {
void speak() { System.out.println("..."); }
}
class Dog extends Animal {
@Override
void speak() { System.out.println("woof"); }
}
class Animal {
void speak() { System.out.println("..."); }
}
class Dog extends Animal {
@Override
void speak() { System.out.println("woof"); }
}
Rust has no classes and no inheritance. Data and behavior are separate. You define a struct for the data:
struct Dog {
name: String,
}
struct Dog {
name: String,
}
And an impl block for the behavior:
impl Dog {
fn speak(&self) {
println!("woof");
}
}
impl Dog {
fn speak(&self) {
println!("woof");
}
}
Shared behavior across types comes from traits, not inheritance:
trait Animal {
fn speak(&self);
}
impl Animal for Dog {
fn speak(&self) {
println!("woof");
}
}
struct Cat;
impl Animal for Cat {
fn speak(&self) {
println!("meow");
}
}
trait Animal {
fn speak(&self);
}
impl Animal for Dog {
fn speak(&self) {
println!("woof");
}
}
struct Cat;
impl Animal for Cat {
fn speak(&self) {
println!("meow");
}
}
Any type can implement any trait. You can write a function that accepts anything implementing Animal:
fn make_noise(animal: &impl Animal) {
animal.speak();
}
fn make_noise(animal: &impl Animal) {
animal.speak();
}
This covers most of what you used inheritance for in Java, without the fragile base class problem. If a parent class changes in Java, every subclass is potentially broken. In Rust, a trait change only affects types that explicitly implement it.
The habit to break: looking for where to put the class. In Rust, you define the data separately from the behavior, and you use traits to share behavior across unrelated types.
5. Assuming the garbage collector handles memory
Java developers rarely think about memory. Objects get created, references get passed around, and the garbage collector cleans up when things are no longer reachable. This works well and you stop thinking about it entirely.
Rust has no garbage collector. Memory is managed through ownership. Every value has exactly one owner. When the owner goes out of scope, the value is dropped and the memory is freed.
fn main() {
let s = String::from("hello"); // s owns this string
let t = s; // ownership moves to t
println!("{}", s); // ERROR: s no longer owns anything
}
fn main() {
let s = String::from("hello"); // s owns this string
let t = s; // ownership moves to t
println!("{}", s); // ERROR: s no longer owns anything
}
This is the thing that breaks Java developers most consistently. In Java, t = s gives both variables a reference to the same object. In Rust, t = s moves ownership. s is no longer valid.
If you want both variables to access the same data, you borrow instead of moving:
fn main() {
let s = String::from("hello");
let t = &s; // t borrows s
println!("{}", s); // fine, s still owns it
println!("{}", t); // fine, t is just a reference
}
fn main() {
let s = String::from("hello");
let t = &s; // t borrows s
println!("{}", s); // fine, s still owns it
println!("{}", t); // fine, t is just a reference
}
The borrow checker enforces that references never outlive the data they point to. There are no dangling pointers. There are no use-after-free bugs. The compiler catches all of this at compile time.
The habit to break: assuming references are just pointers to shared data. In Rust, passing a value without & moves it. The original variable is gone. If you want the original to remain valid, you pass a reference.
The pattern across all five
Every one of these habits comes from the same place: Java makes a decision for you and you never have to think about it. Null is allowed. Mutation is allowed. Exceptions happen out of band. Memory is managed for you. Classes bundle everything together.
Rust makes you state these decisions explicitly. Absence is Option. Mutation is mut. Failure is Result. Memory management is ownership. Behavior is traits.
This feels like more work at first. After a while it feels like the code is just more honest about what it is actually doing.
What helped me most
Reading error messages carefully. The Rust compiler is unusually good at telling you exactly what is wrong and often suggesting the fix. When you hit a borrow checker error, do not guess. Read the message, understand why Rust is objecting, and then fix it. Guessing your way past the compiler teaches you nothing.
The second thing: stop trying to translate Java patterns directly. A singleton in Java becomes a module-level constant or OnceLock in Rust. A factory method becomes an associated function. An abstract class becomes a trait. The mapping is not one-to-one but the concepts are there once you stop looking for the Java version.
I spent six months writing about Rust and turned it into a 23-chapter field guide with a full Redis clone as the capstone. If you are working through this language seriously, it covers everything from ownership to production-level concurrency.
$7 — Zero to Rust: A Systems Programmer’s Field Guide
You can read the free sample here — Free 20-page sample
The ownership and borrowing section above only scratches the surface. If you want the full picture on how Rust handles memory without a garbage collector, I covered it in depth here:
Ownership: The One Rule That Makes Rust Different From Everything Else
Not a medium member yet? Read it here for free

