Giter Site home page Giter Site logo

learn_rust's Introduction

Hello Rust

本仓库旨在入门Rust基础语法(基于Rust中文教程),并且横向比较同类型语法在Java和Python中的实现

基础

hello world

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

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')

for

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))

while

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())        

learn_rust's People

Contributors

kaixinbaba avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.