Learn Rust With Java, Part 1: The Introduction
This is the first post in the Learn Rust with Java series. This blog will enable you to have a first look into the Rust world on a JFO (Java Flying Object).
Join the DZone community and get the full member experience.
Join For FreeHi everyone!!!
This series of blogs to quickly learn the Rust language is my first blog after several attempts at writing :). This post might help Java developers who want to learn Rust as their next language. It will be a comparative study of the language features and implementation methodologies between Rust and Java. I also recommend the Rust Book for better learning.
The Super-Hero Introduction?
Here is how the story starts: Java is the language I love most from my higher education. After 10+ years of development in Java, I started looking for an impressive alternate programming language. I tried Python, but it did not work well for me. I then considered the Go language, but it does require runtime and garbage collector like Java. Next, I got introduced to Scala as part of my work project, but I felt it was a language with complicated programming patterns and multi-versed programming methodologies still running on Java Virtual Machine.
I have special interests in Operating Systems, even though I am not a system or kernel programmer. With that said, I am not impressed with Android OS because of its User Interface and privacy issues. However, I am a huge fan of the Windows Zune-themed Metro User Interface of the Windows Phone for its fluidity and user experience. All of that introduction leads up to explain that I am writing this blog on Fedora Linux.
Some time ago, I learned that Google was working on a new OS: Fuchsia. In Fuchsia’s Wiki page, I came across a new language(to me actually) “written in” called “Rust”. When I explored its features, it was like watching a superhero movie trailer. Of course, I am a multi-genre movie fan from Star Wars to Martin Scorsese films.
It's tech time now.
Installation and Tool-Chain
Installation is usual for both languages. The JavaSE installs Java Development Kit and Java Runtime Environment, while in the case of Rust, the rustup
installs the Rust language under /.cargo
directory with the below set of tools.
cargo cargo-fmt clippy-driver rustc rustfmt rust-lldb
cargo-clippy cargo-miri rls rustdoc rust-gdb rustup
Here, the tool-chain rustup
is the one such important tool that Java lacks. For example, the JDK update process is not developer-friendly, but in Rust, the rustup
does a lot of things, from installation to update and adding additional components.
To update Rust:
rustup --update
IDE
Every developer loves to work on an IDE: for Java, it will be IntelliJ and Eclipse mostly. In the case of Rust, there is no full-featured IDE. Options include VSCode with CodeLLDB debugger, IntelliJ Community Edition without debugger, IntelliJ Ultimate/CLion(I have not explored this option), and multiple text editors. Now Fleet from JetBrains, a VS Code competitor, also comes with Rust support.
Classes/Files
Java’s Object-Oriented Programming Model involves class as the base concept. The Rust language also supports Object Orientation but in a different manner, which we will talk about later. It is the *.java
file extension in Java and *.rs
extension in Rust.
Compilation
In Java, every class/file is first compiled and then executed using javac
and java
commands, respectively. In a similar way, Rust files are first compiled with rustc.
Once compiled, it turns into LLVM bit code, and then further compiled into machine code so it does not need any runtime.
Due to its compile-time evaluation for memory safety and optimization, it will take a longer time to compile than Java. Rust supports two modes of compilation with its build tool cargo
(will see in detail when discussing build tool). The dev
mode is a non-optimized, quicker compilation, whereas the release
mode is an optimized, slower compilation only done during the release phase of the software application.
Runtime
Java requires its runtime, Java Virtual Machine, to be installed in the execution environment. Rust does not need any runtime.
Java has a garbage collector. Rust does not need a garbage collector, which adds a great advantage to its improved performance.
Build Tool
Java does not have a built-in full-featured build tool. Developers usually prefer open-source alternatives like Gradle or Maven. However, Rust has a powerful built-in build too called cargo
that also acts as a package manager similar to the mvn
.
In Java:
The library/binary packages of source files are called JAR files with *.jar
extension.
To create a new Java project in Maven:
mvn archetype:generate -DgroupId=com.tutorial -DaritifactId=hello-world -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
To build a Maven Java Project:
mvn clean package
To run a Maven project:
mvn clean compile exec:java
In Rust:
The library/binary packages of source files are called crates
with *.crate
file extensions.
To create a new Rust project in Cargo:
cargo new hello-world
To build and run a Rust project:
cargo run
To check a Rust project compilation:
cargo check
To build a Rust project:
cargo build
To build a Rust project for release:
cargo build --release
Dependency Management
In Maven, the Project Object Model pom.xml
is the source for Maven build and dependency management. Similarly in Rust, Cargo.toml
is the source file for cargo to build, resolve dependencies, and package.
In Maven dependency declaration:
...
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
<version>1.0</version>
<type>bar</type>
<scope>runtime</scope>
</dependency>
</dependencies>
...
In Cargo dependency management:
...
[dependencies]
rand = "0.8.0"
...
Hello World!
In Java:
class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
In Rust:
fn main(){
println!("Hello World!");
}
Project Structure
Below is the complete package layout reference from the Cargo book:
.
├── Cargo.lock
├── Cargo.toml
├── src/
│ ├── lib.rs
│ ├── main.rs
│ └── bin/
│ ├── named-executable.rs
│ ├── another-executable.rs
│ └── multi-file-executable/
│ ├── main.rs
│ └── some_module.rs
├── benches/
│ ├── large-input.rs
│ └── multi-file-bench/
│ ├── main.rs
│ └── bench_module.rs
├── examples/
│ ├── simple.rs
│ └── multi-file-example/
│ ├── main.rs
│ └── ex_module.rs
└── tests/
├── some-integration-tests.rs
└── multi-file-test/
├── main.rs
└── test_module.rs
Cargo.toml
andCargo.lock
are stored in the root of your package (package root).- Source code goes in the
src
directory. - The default library file is
src/lib.rs
. - The default executable file is
src/main.rs
. - Other executables can be placed in
src/bin/
. - Benchmarks go in the
benches
directory. - Examples go in the
examples
directory. - Integration tests go in the
tests
directory.
We will dive deep into the Project structure, Crates, and Module(in Java terms package) in later blogs.
That concludes this introduction to the Rust language. Will see you in the next blog.
Opinions expressed by DZone contributors are their own.
Comments