Hier sind Beispiele für einfache Programme in Java und Rust, die ähnliche Funktionen erfüllen:
1. Hallo-Welt-Programm
Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hallo, Welt!");
}
}
Rust
fn main() {
println!("Hallo, Welt!");
}
2. Berechnung der Fakultät (rekursiv)
Java
public class Factorial {
public static int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
public static void main(String[] args) {
int number = 5;
System.out.println("Fakultät von " + number + " ist: " + factorial(number));
}
}
Rust
fn factorial(n: u32) -> u32 {
if n <= 1 {
1
} else {
n * factorial(n - 1)
}
}
fn main() {
let number = 5;
println!("Fakultät von {} ist: {}", number, factorial(number));
}
3. Einfache Struktur (Klasse in Java, Struct in Rust)
Java
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void introduce() {
System.out.println("Hallo, ich bin " + name + " und bin " + age + " Jahre alt.");
}
public static void main(String[] args) {
Person person = new Person("Max", 30);
person.introduce();
}
}
Rust
struct Person {
name: String,
age: u32,
}
impl Person {
fn introduce(&self) {
println!("Hallo, ich bin {} und bin {} Jahre alt.", self.name, self.age);
}
}
fn main() {
let person = Person {
name: String::from("Max"),
age: 30,
};
person.introduce();
}
4. Datei lesen und ausgeben
Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("test.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println("Fehler beim Lesen der Datei: " + e.getMessage());
}
}
}
Rust
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn main() -> io::Result<()> {
let path = Path::new("test.txt");
let file = File::open(&path)?;
let reader = io::BufReader::new(file);
for line in reader.lines() {
println!("{}", line?);
}
Ok(())
}
5. Einfacher HTTP-Server (mit externen Bibliotheken)
Java (mit Spark Framework)
import static spark.Spark.*;
public class SimpleServer {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hallo, Welt!");
}
}
Rust (mit Actix-Web)
use actix_web::{get, App, HttpServer, Responder};
#[get("/hello")]
async fn hello() -> impl Responder {
"Hallo, Welt!"
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(hello))
.bind("127.0.0.1:8080")?
.run()
.await
}
Zusammenfassung der Unterschiede
| Aspekt | Java | Rust |
|---|
| Speicherverwaltung | Garbage Collection | Ownership-Modell |
| Fehlerbehandlung | Exceptions | Result und Option |
| Performance | JVM-Overhead | Nahe an C/C++ |
| Sicherheit | NullPointer, Race Conditions | Keine NullPointer, sichere Nebenläufigkeit |
| Syntax | Objektorientiert | Systemnah, funktionale Elemente |
Falls du spezifischere Beispiele brauchst, lass es mich wissen! 😊