本仓库旨在入门Rust基础语法(基于Rust中文教程),并且横向比较同类型语法在Java和Python中的实现
fn main() {
println!("hello world");
}
public static void main(String[] args) {
System.out.println("hello world");
}
print('hello world')
// 单行注释
///文档注释
///支持markdown
///
//!另一种文档注释
//!
//!
// 单行注释
/*
* 多行注释
*/
/**
* 文档注释
*/
# 单行注释
'''
多行注释
'''
"""
多行注释
"""
// 加了mut才是意义上的变量
let mut number = 7;
// 不能被重新赋值的变量
let symbol = "abc";
// 指定类型
let x: i32 = 1;
let f: bool = true;
int number = 7;
int x = 1;
boolean f = true;
number = 7
x = 1
f = True
const VALUE = 100;
final int VALUE = 100;
# python没有常量的定义
VALUE = 100;
if number < 5 {
println!("condition was < 5");
} else if number > 7 {
println!("condition was > 7");
} else {
println!("other condition");
}
if (number < 5) {
System.out.println("condition was < 5");
} else if (number > 7) {
System.out.println("condition was > 7");
} else {
System.out.println("other condition");
}
if number < 5:
print('condition was < 5')
elif number > 7:
print('condition was > 7')
else:
print('other condition')
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!("the value is: {}", element);
}
List<Integer> a = new ArrayList<Integer>();
for (Integer element : a) {
System.out.println(String.format("the value is : %s", element));
}
a = [10, 20, 30, 40, 50]
for element in a:
print('the value is : {}'.format(element))
let mut number = 3;
while number != 0 {
println!("{}!", number);
number = number - 1;
}
///////////
loop {
println!("again!");
}
Integer number = 3;
while (number != 0) {
System.out.println(String.format("%s!", number));
number--;
}
///////////
while (true) {
System.out.println("again!");
}
number = 3
while number != 0:
print('{}!'.format(number))
number = number - 1
##########
while True:
print('again!')
fn method1() {
}
pub fn method2() -> i32 {
}
fn method3(x: i32) -> i64 {
}
pub fn method4(x: i32, y: bool) -> f64 {
}
void method1() {
}
public int method2() {
}
long method3(int x) {
}
public double method4(int x, boolean y) {
}
def method1():
pass
def method2() -> int:
pass
def method3(x: int) -> int:
pass
def method4(x: int, y: bool) -> float:
pass
# python有默认参数和关键字参数,而且其实并不需要声明返回值
def method(x=None, y=None):
pass
struct Person {
age: i32,
name: String,
}
class Person {
int age;
String name;
}
class Person(object):
def __init__(self, age=None, name=None):
self.age = age
self.name = name
struct Rectangle {
width: i32,
height: i32,
}
impl Rectangle {
fn area(&self) -> i32 {
self.width * self.height
}
fn create(size: i32) -> Rectangle {
Rectangle { width: size, height: size }
}
}
fn main() {
let rec1 = Rectangle::create(5);
// 25
println!(rec1.area());
}
class Rectangle {
int width;
int height;
Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
int area() {
return this.width * this.height;
}
static Rectangle create(int size) {
return new Rectangle(size, size);
}
}
public static void main(String[] args) {
Rectangle rec1 = Rectangle.create(5);
// 25
System.out.println(rec1.area());
}
class Rectangle(object):
def __init__(self, width, height):
self.width = width
self.height = height
def area():
return self.width * self.height
@staticmethod
def create(size):
return Rectangle(size, size)
if __name__ == '__main__':
rec1 = Rectangle.create(5)
# 25
print(rec1.area())