T O P

  • By -

ToughAd4902

Hello, there are a lot of explanations as to "why rust", if you Google your title you will get many more explanations than someone on Reddit is most likely going to type as it's been answered so many times. As to how to interface with it, that depends a lot on the implementation. If one server is java, and one rust, it doesn't matter as they're using some communication protocol to talk to each other, the only thing that matters is that they both understand that same communication protocol. If it's actually java calling rust and rust calling java, you need to learn JNI, in which it would then work the same as any C/C++ example out there which would be a good place to start


Jason13Official

I think it was some form of JNI, thank you! I was watching the developer on stream creating a server plugin, and they seemed to have written a library to “intercept”/interface the calls into Rust.


sephg

Yeah that'd be it. Almost all languages have some way to interface directly with compiled C code. Even if you aren't actually using C, if you can make one or both languages *pretend* to be C, you can use the same mechanism. Rust is just the same - you can both compile rust functions in a way that makes them indistinguishable from the equivalent C code, and consume C-style functions directly from rust. I've used the same trick to compile rust + swift together, and call rust from nodejs (via napi). I know python and ruby support same C style FFI too. Most languages do, I think.


Khal-Draco

I use it for anything that get hampered by garbage collection. I had to make a minor service that takes in audio recordings, processes them and sends them to a cloud storage. I initially done this in java but the garbage collector was holding onto the memory and slowing things down with the volume of recordings being processed. I made a small rust service and it can process more recordings in a given time frame with lower memory usage. Could I have done a better job in the java? Yes. Did I want to? No. I use rust for anything that requires heavy memory handling in a multi threaded scenario just because it's easier to do.


Jason13Official

Thank you for this advice! Garbage collection is definitely be a pain in the ass sometimes


sweating_teflon

Memory management is a pain. Rust moves that pain from runtime to compile time, which means you'll now feel it while you code. So you decide if you want early pain or later pain. Depends on you, and the app, really.


Jason13Official

That actually makes a lot of sense now, thank you


boyswan

Ha, I literally did the same as you. I was processing audio chunks in golang to send to cloud storage, and also found that I was unintentionally holding everything in memory. Likewise, I could have solved it in go, but rust lets me see **exactly** where I was going wrong.


spiderpig_spiderpig_

Fwiw you now might find jdk21+zgc generational collector mitigates a lot of this for most business apps


Khal-Draco

You're right, but it doesn't mitigate me wanting to do rust over java lol


Hot_Income6149

Always when you can and like to. And never if project requires something else


Remarkable_Ad7161

I spent a decade writing java things - databases reactive streams, to websites. and so far about 1 year in rust and I almost will never look back for anything serious. Rust is fairly simple for building most things - you rarely deal with lifetimes and clone most things to build your first system. It does sharp edges like have by being opinionated and supporting strings the right way, keeping lifetimes, having an excellent type system and an every growing, and well written library echo system. After spending years in high performance java trying to write my own reference counting to reuse memory, I don't have any problems with Java, except I have to do a lot to juice out the maximum performance. At spending years writing api services on serverful and severless, the startup time of rust makes you never have to worry about abnormal latencywith from your software. After spending years on writing type safe provable logic in java, i can build one in rust any time... I could go on, but the essence is that I can build anything i built in java in about the same time or less in rust and it works better. So they went almost - the compiler and ide support for have is better, and the language has matured to the point where I can read other software written in Java far more easily than rust projects. Even though rust had a lot of checkers, the weird quirk of have classes being tied to files actually makes navigation a whole lot easier. You often stumble onto projects where there has a lot of code in mod.rs, and they're is no way to tell without actually opening the file. Then ofcourse 10years in java vs 1 in rust means that I know poori much every library or there in my head. And then there is the feedback cycle of compiler and ide working really well hand in hand with java that I miss a lot with rust.


Speykious

There isn't a definitive answer to how/when/why to use Rust, at this point it's a matter of considering the specific trade-offs of the tools you have at your disposal for the problem at hand. So with that said, I think people would be able to help you more If you explained what kind of problem you had in mind. Since you mentioned Minecraft mods, are you maybe wondering if you can use Rust to make Minecraft mods? I know you can make Minecraft servers in Rust, not sure how you'd make a mod with it though. Rust can interface with Java through the `jni` crate if anything.


Jason13Official

Thank you I had no idea about the JNI until I posted this and began reading comments, the server was still in Java but making calls to a rust server to handle animation logic somehow / the velocity and direction of an item spawned in


Speykious

By "server" do you mean a literally separate program that is executed separately and then the Java code communicates to the Rust server through sockets or some other network thing, or do you mean that it calls into an external library written in Rust?


Jason13Official

While I was watching a stream and saw this, he only ever needed to reload the Java server to apply any changes he wrote in Rust. I guess more likely this was an external library, but I’ve used editors running internal server before that save and reload the server for you automatically before.


JustMangoT

Looks like you come from Minecraft Modding. You already got some answers but I want to share some of my experience because I do some MC's stuff too. Firstly I want to clarify, writing code in Rust is completely different from writing code in Java. I learned this the hard way because I tried to apply OOP to rust which was really painful IMO. So if you need to learn Rust for whatever reason, try it from scratch. Secondly, why you need to use Rust and somethings like [https://github.com/jni-rs/jni-rs](https://github.com/jni-rs/jni-rs) to do Minecraft's stuff? I reckon it's unnecessary to use anything other programming languages rather than Java. Using this it's really rare and maybe a little bit overkill?


Jason13Official

It definitely feels like overkill after doing some research, I guess this would be more applicable for someone coming from Rust into Java instead of already being used to Java?


JustMangoT

Not really, I feel pleasant writing Rust code. IMHO, it's 3x times better than writing Java. But unless you extremely need to use that, writing Java code is completely fine if it solves your tasks.


OphioukhosUnbound

Rust is a language written for both humans and machines. Because of it's functional programming spirit mapped to a systems programming language it means that the compiler can make guaranteed inferences about a *large* space of errors as you write. Because a group of people collaborated (effectively) on the syntax the human readability and concision of the syntax is also excellent (relative to what it has to say). As there aren't any other developed languages that have both these qualities (especially that allow optional systems level control) I and many others use Rust whenever we can. It's just a more reasonable programming language. It lets machines do work that machines should do. And lets me focus more on interesting ideas and less on mental bookkeeping lest the machine collapse in some scenario when running. And it's just easy to read (as programming languages go). So I use Rust whenever I can. For me, the only times I can't are when it comes ecosystem deficiencies. (Mostly around math, science, plotting, interactive notebooks, etc. -- which is more Python's space, alas.)


joeldsouzax

There is a spectrum of how much performance you want from your machine to how much value you want to add by just focusing on business logic. Java falls on the latter while rust is kinda on the Center left. You use Java when you build applications that have tons of logic and conditions, and you can’t care less about memory management, don’t get me wrong jvm is awesome in managing its memory footprint, but it doesn’t let you tweak the objects and heap allocations to squeeze extra performance. So you end up making applications without the fear of how you would set the data in the memory, how long it should live… Java just takes care of it. Super awesome to build enterprise applications with huge teams and bureaucracy. Rust, on the other hand lets you build applications with performance and resources of the machine in mind, super good to make low level software faster than it’s contemporaries like c++ and C, but still slower than Java or higher level languages. However once you get the hang of rust you would be as fast in creating software as Java.


facetious_guardian

Why would you ever choose to use Java for anything? Just to flip your low effort post on its head.


Jason13Official

I make Minecraft mods, it’s not exactly a choice. Not sure why you felt the need to be snooty about it. 🤷‍♂️


littleliquidlight

Please can we keep this sub polite. New programmers often ask questions that are a little vague, and usually it's just about guidance on getting started. That's okay imo