Thanks for the fast and informative answers! So let me try to summarize: Rust collections lay out their elements in a contiguous area of the heap, whereas Java collections only store references to elements that may be scattered across the heap. This improves cache locality and reduces the number o… Answer from alexanderkoller on users.rust-lang.org
🌐
JetBrains
blog.jetbrains.com › rust › 2025 › 08 › 01 › rust-vs-java
Rust vs. Java: Choosing the Right Tool for Your Next Project | The RustRover Blog
February 17, 2026 - While Rust is praised for its focus on safety and performance, its complexity presents a challenge to adopters (arguably, the steep learning curve is why so many developers love it!). In contrast, Java remains a cornerstone language thanks to ...
🌐
Reddit
reddit.com › r/rust › rust vs java: a staff engineer's perspective
r/rust on Reddit: Rust vs Java: A Staff Engineer's perspective
January 2, 2023 - Duke vs Ferris

Rust and Java are two of the most popular programming languages, but which one is best for your next project?

I put together a cheatsheet to answer this:

Source code: https://github.com/security-union/rust-vs-java

Html version: https://security-union.github.io/rust-vs-java/

Also I created a video showing interesting aspects of both languages: https://youtu.be/-JwgfNGx_V8

Java vs Rust cheatsheet
Discussions

Java vs Rust performance - Stack Overflow
I don't find this a surprising result, there's no allocation here so GC doesn't matter, and Java's JIT compiler is very good at optimized simple code as this. ... I adjusted your code to eliminate the points of criticism laid out in the comments. Not compiling Rust for production is the biggest ... More on stackoverflow.com
🌐 stackoverflow.com
Java Is Better Than Rust
Oh lordy please no. That's how we got into so many messes in the first place. I think writing Rust made me a better C programmer by drawing my attention to how many bad things I was doing that a C compiler would cheerfully compile without saying a word. Oh, you want to malloc a chunk of heap, ... More on news.ycombinator.com
🌐 news.ycombinator.com
7
20
July 23, 2024
Why Java program is faster than Rust in this comparision?
hi , this is a benchmark Between Java and Rust : Rust result : 10648, 6678, 8274 Java result : 8661, 9608, 6302 avg of 12 times benchmark Rust : 9948 Java : 8693 I used opt-level = 3 for Rust Rust Code fn main(… More on users.rust-lang.org
🌐 users.rust-lang.org
1
0
February 16, 2022
Rust is a nightmare to learn coming from Java
& * 'a &'a Box || macro! mod crate extern crate #[...] String vs &str &self unwrap expect ? Rc Arc I was so happy and excited with my first println!("hello") program but more and more things get complex till a point i turn crazy Guys, how are you able to understand such a complex programming lang ? More on users.rust-lang.org
🌐 users.rust-lang.org
19
6
January 30, 2020
Top answer
1 of 2
17

I adjusted your code to eliminate the points of criticism laid out in the comments. Not compiling Rust for production is the biggest problem, that introduces a 50x overhead. Beyond that, I eliminated printing while measuring, and did proper warming up of the Java code.

I would say that Java and Rust were on par after these changes, they are within 2x of each other and both have very low cost per iteration (just a fraction of a nanosecond).

Here is my code:

public class Testing {
    private static final int NUM_ITERS = 1_000;
    private static final int MEASURE_TIMES = 7;

    public static void main(String[] args) {
        for (int i = 0; i < MEASURE_TIMES; i++) {
            System.out.format("%.2f ns per iteration%n", benchmark());
        }
    }

    private static double benchmark() {
        long tInit = System.nanoTime();
        int c = 0;
        for (int i = 0; i < NUM_ITERS; ++i) {
            for (int j = 0; j < NUM_ITERS; ++j) {
                for (int k = 0; k < NUM_ITERS; ++k) {
                    if (i*i + j*j == k*k) {
                        ++c;
                    }
                }
            }
        }
        if (c % 137 == 0) {
            // Use c so its computation can't be elided
            System.out.println("Count is divisible by 13: " + c);
        }
        long tookNanos = System.nanoTime() - tInit;
        return tookNanos / ((double) NUM_ITERS * NUM_ITERS * NUM_ITERS);
    }
}
use std::time::SystemTime;

const NUM_ITERS: i32 = 1000;

fn main() {
    let mut c = 0;

    let t_init = SystemTime::now();
    for i in 0..NUM_ITERS {
        for j in 0..NUM_ITERS {
            for k in 0..NUM_ITERS {
                if i*i + j*j == k*k {
                    c += 1;
                }
            }
        }
    }
    let took_ns = t_init.elapsed().unwrap().as_nanos() as f64;

    let iters = NUM_ITERS as f64;
    println!("{} ns per iteration", took_ns / (iters * iters * iters));
    // Use c to ensure its computation can't be elided by the optimizer
    if c % 137 == 0 {
        println!("Count is divisible by 137: {}", c);
    }
}

I run Java from IntelliJ, with JDK 16. I run Rust from the command line, using cargo run --release.

Example of Java output:

0.98 ns per iteration
0.93 ns per iteration
0.32 ns per iteration
0.34 ns per iteration
0.32 ns per iteration
0.33 ns per iteration
0.32 ns per iteration

Example of Rust output:

0.600314 ns per iteration

While I'm not necessarily surprised to see Java giving a better result (its JIT compiler has been optimized for 20 years now and there's no object allocation, so no GC), I was puzzled at the overall low cost of an iteration. We can assume the expression i*i + j*j to be hoisted out of the inner loop, which leaves just k*k inside it.

I used a disassembler to check out the code Rust produced. It definitely involves IMUL in the innermost loop. I read this answer, which says Intel has a latency of just 3 CPU cycles for an IMUL instruction. Combine that with multiple ALUs and instruction parallelism, and the result of 1 cycle per iteration becomes more plausible.

Another interesting thing I discovered is that, if I just check c % 137 == 0 but don't print the actual value of c in the Rust println! statement, (only print "Count is divisible by 137"), iteration cost drops to just 0.26 ns. So Rust was able to eliminate a lot of work from the loop when I didn't ask for the exact value of c.


UPDATE

As discussed in the comments with @trentci, I mimicked the Java code more completely, adding an outer loop that repeats the measurement, which is now in a separate function:

use std::time::SystemTime;

const NUM_ITERS: i32 = 1000;
const MEASURE_TIMES: i32 = 7;

fn main() {
    let total_iters: f64 = NUM_ITERS as f64 * NUM_ITERS as f64 * NUM_ITERS as f64;
    for _ in 0..MEASURE_TIMES {
        let took_ns = benchmark() as f64;
        println!("{} ns per iteration", took_ns / total_iters);
    }
}

fn benchmark() -> u128 {
    let mut c = 0;

    let t_init = SystemTime::now();
    for i in 0..NUM_ITERS {
        for j in 0..NUM_ITERS {
            for k in 0..NUM_ITERS {
                if i*i + j*j == k*k {
                    c += 1;
                }
            }
        }
    }
    // Use c to ensure its computation can't be elided by the optimizer
    if c % 137 == 0 {
        println!("Count is divisible by 137: {}", c);
    }
    return t_init.elapsed().unwrap().as_nanos();
}

Now I'm getting this output:

0.781475 ns per iteration
0.760657 ns per iteration
0.783821 ns per iteration
0.777313 ns per iteration
0.766473 ns per iteration
0.774042 ns per iteration
0.766718 ns per iteration

Another subtle change to the code that resulted in a significant change in performance. However, it also shows a key advantage of Rust over Java: there is no warmup needed to get the optimum performance.

2 of 2
3

The Java VM possibly just-in-time compiles your inner loop to native assembly code after it exceeds a certain number of iterations. I am not an JVM expert but this could possibly explain why a larger number of iterations magically makes the java code run faster.

The technology behind this is called HotSpot and afaik it works differently on "client" and "server" VMs. It also depends on the vendor of the JVM if it is available and how it behaves.

https://en.wikipedia.org/wiki/HotSpot_(virtual_machine)

🌐
StackShare
stackshare.io › stackups › java-vs-rust
Java vs Rust | What are the differences? | StackShare
This enables Rust to provide memory safety without relying on garbage collection. Concurrency: Java follows a thread-based concurrency model, where multiple threads of execution can be created and run concurrently. Rust, on the other hand, follows an "ownership" based concurrency model, which is thread-safe and allows for efficient concurrency without the risk of data races.
🌐
Medium
keazkasun.medium.com › before-moving-to-rust-from-java-2b87a70654c0
Before moving to Rust from Java | by Kasun Ranasinghe | July 2023 | Medium
December 4, 2023 - With Rust, you can write concurrent applications more easily than Java. You have two options, Message passing and Shared concurrency(one we love to hate). Normally, shared concurrency is difficult to implement. In Java, there is no way to know whether that object is shared between threads or not just by looking at the variable and there are no compiler restrictions to share a variable between threads also.
🌐
Hacker News
news.ycombinator.com › item
Java Is Better Than Rust | Hacker News
July 23, 2024 - Oh lordy please no. That's how we got into so many messes in the first place. I think writing Rust made me a better C programmer by drawing my attention to how many bad things I was doing that a C compiler would cheerfully compile without saying a word. Oh, you want to malloc a chunk of heap, ...
Find elsewhere
🌐
Infinyon
infinyon.com › resources › files › java-vs-rust.pdf pdf
LinkedIn | Twitter | Github Java vs. Rust Comparison
Rust · delivers faster startup times and smaller memory footprint on top of it. Java uses Garbage
🌐
Quora
quora.com › What-are-the-benefits-of-learning-Java-or-Rust-programming-languages-Which-one-do-you-think-would-be-more-beneficial-for-career-growth-in-the-future
What are the benefits of learning Java or Rust programming languages? Which one do you think would be more beneficial for career growth in the future? - Quora
Answer: Hello, Rust and Java are same difficulty level. Rust is modern Java is old. Java programmers get fired, Rust programmers get hired. The choice is easy. However both Rust and Java are difficult languages and some companies will prefer ...
🌐
Rayobyte
rayobyte.com › blog › rust-vs-java
Java vs. Rust: Read Before Choosing Either
March 6, 2025 - The latter is a newer player, and it prioritizes safety and performance in system-level programming. The Rust vs. Java comparison depends on a project’s requirements, but the decision often boils down to analyzing the different trade-offs between the two languages.
🌐
Programming Language Benchmarks
programming-language-benchmarks.vercel.app › rust-vs-java
Rust VS Java benchmarks, Which programming language or compiler is faster
Javascript · Kotlin · Lua · Nim · OCaml · Odin · Perl · Php · Python · Ruby · Rust · Swift · Typescript · V · Wasm · Zig · Current benchmark data was generated on Fri Aug 01 2025, full log can be found HERE · CONTRIBUTIONS are WELCOME! CPU INFO:[x86_64][4 cores] AMD EPYC 7763 64-Core Processor (Model 1) * -m in a file name stands for multi-threading or multi-processing ·
🌐
Medium
medium.com › @rs4528090 › rust-vs-java-which-one-to-choose-64bb7b62122c
Rust vs Java: Which One to Choose? | by Rahul Sharma | Medium
July 23, 2024 - When it comes to performance, Rust often has the upper hand due to its system-level capabilities and zero-cost abstractions. Java, while optimized for performance, can be limited by the overhead of the JVM and garbage collection.
🌐
Llogiq
llogiq.github.io › 2016 › 02 › 28 › java-rust.html
Comparing Rust and Java — Llogiq on stuff
February 28, 2016 - The first obvious difference between Rust and Java is that the latter runs on the JVM, so it’s just-in-time compiled. This means that Java can benefit from profile-based optimizations that in theory allow better performance than compile-time optimized code for some workloads.
🌐
Opensource.com
opensource.com › article › 20 › 6 › why-rust
Why I switched from Java to Rust | Opensource.com
It's almost impossible to stop people doing the wrong thing entirely, but encouraging people to do the right thing is great. In fact, Rust goes further and actually makes it difficult to do the wrong thing in many situations.
🌐
Medium
medium.com › deno-the-complete-reference › java-vs-rust-how-faster-is-machine-code-compared-to-interpreted-code-for-jwt-sign-verify-fa6aeeabff58
Java vs Rust: How faster is machine code compared to interpreted code for JWT sign & verify? | Tech Tonic
November 18, 2023 - Ultimately, a noteworthy contender emerges to challenge the prowess of Rust. When it comes to the JWT application, Rust outpaces Java by a substantial margin, exhibiting a performance that is five times swifter.
🌐
Medium
medium.com › @logicoverlatte › java-vs-rust-the-showdown-nobody-wants-to-admit-the-outcome-of-c3e8a1eed3be
Java vs Rust: The Showdown Nobody Wants to Admit the Outcome Of | by Logic Over Latte | Medium
August 29, 2025 - And now Rust, the shiny new darling of systems programming. But curiosity got me. I decided to actually test them side by side. And what I found? Well, let’s just say this “showdown” isn’t as clear-cut as Twitter debates would have you believe. It’s late at night, coffee on my desk, and I spin up two projects — one in Java 21, one in Rust.
🌐
FreshersNow
freshersnow.com › home › blog › top 50 differences between rust and java | rust vs java
Top 50 Differences Between Rust and Java | Rust Vs Java
March 7, 2023 - Rust has gained popularity for its memory safety, system-level programming, and performance, while Java is widely used in enterprise applications and offers cross-platform compatibility.