Rust programming for Java developers

Among the newer programming languages growing in popularity is Rust. Rust was first introduced in 2010 and has quietly gained mindshare for its performance, syntax, and thread safety features. If you are a Java developer, you’ll find Rust relatively easy to get a grip on, thanks to the similarity of the two languages.

Rust has climbed the ladder of language popularity, or most commonly used languages, but most tellingly, Rust continually tops out as the the “most loved language” of all, according to the Stack Overflow survey. That is a testament to the great experience of using Rust.

Also on InfoWorld: What is Rust? Safe, fast, and easy software development ]

Read on for a look at some of the main things to know about Rust if you’re coming from a Java background.

Rust syntax

Like Java, Rust is compiled. It is compiled to the LLVM spec, similar in spirit to the JVM, allowing for output to a variety of target platforms.

And like Java, Rust descends from the C lineage. Its use of curly braces for blocks and semi-colons for line terminations is exactly the same as Java. For example, you can see a simple program here, like Listing 1.

Listing 1. Simple Rust code

fn main() { println!("Hello, InfoWorld!"); }

Notice that there is a main()function, similar to the entry point in Java.


Functions in Rust

Functions stand alone in Rust, and they can be declared anywhere, including nested in other functions. This is unlike Java, where functions are always declared as methods on objects (except in the case of lambdas). Put another way, in Java everything is an object. Not so in Rust.

[ REGISTER NOW for CIO 100: Symposium & Awards Conference, August 15-17 ]

Listing 2. Using functions in Rust

Find out more about this interesting topic here.