// function
/*add*/
fn add(a: i32, b: i32) -> i32 {
return a + b;
}
fn another_function(x: i32, y: i32) {
println!("x 的值为 : {}", x);
println!("y 的值为 : {}", y);
}
fn main() {
another_function(5, 6);
println!("{}",add(2,3));
let x = 5;
let y = {
let x = 3;
x + 1
};
println!("x 的值为 : {}", x);
println!("y 的值为 : {}", y);
fn five() -> i32 {
5
}
println!("five() 的值为: {}", five());
let a = 12;
let b;
if a > 0 {
b = 1;
} else if a < 0 {
b = -1;
} else {
b = 0;
}
println!("b is {}", b);
let a = 3;
let number = if a > 0 { 1 } else { -1 };
println!("number 为 {}", number);
let mut number = 1;
while number <= 4 {
println!("{}", number);
number += 1;
}
println!("EXIT");
let a = [10, 20, 30, 40, 50];
for i in a.iter() {
println!("值为 : {}", i);
}
let a = [10, 20, 30, 40, 50];
for i in 0..5 {
println!("a[{}] = {}", i, a[i]);
}
let s = ['R', 'U', 'N', 'O', '0', 'B'];
let mut i = 0;
loop {
let ch = s[i];
if ch == '0' {
break;
}
println!("\'{}\'", ch);
i += 1;
}
let s1 = String::from("hello");
let s2 = s1;
println!("{}, world!", s2);
let s1 = String::from("hello");
let s2 = s1.clone();
println!("s1 = {}, s2 = {}", s1, s2);
let s = String::from("broadcast");
let part1 = &s[0..5];
let part2 = &s[5..9];
println!("{}={}+{}", s, part1, part2);
let arr = [1, 3, 5, 7, 9];
let part = &arr[0..3];
for i in part.iter() {
println!("{}", i);
}
/*
struct Site {
domain: String,
name: String,
nation: String,
found: u32
}
let domain=String::from("www.runoob.com");
let name=String::from("RUNOOB");
let runoob = Site {
domain,
name,
nation: String::from("China"),
found: 2013
};
*/
struct Color(u8, u8, u8);
struct Point(f64, f64);
let black = Color(0, 0, 0);
let origin = Point(0.0, 0.0);
println!("black = ({}, {}, {})", black.0, black.1, black.2);
println!("origin = ({}, {})", origin.0, origin.1);
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn create(width: u32, height: u32) -> Rectangle {
Rectangle { width, height }
}
fn area(&self) -> u32 {
self.width * self.height
}
fn wider(&self, rect: &Rectangle) -> bool {
self.width > rect.width
}
}
let rect1 = Rectangle::create(30, 50);
let rect2 = Rectangle { width: 40, height: 20 };
println!("rect1's area is {}", rect1.area());
println!("{}", rect1.wider(&rect2));
enum Book {
Papery {index: u32},
Electronic {url: String},
}
let _book = Book::Papery{index:1001};
let _ebook = Book::Electronic{url: String::from("url...")};
match _book {
Book::Papery { index } => {
println!("Papery book {}", index);
},
Book::Electronic { url } => {
println!("E-book {}", url);
}
}
let t = "abc2";
match t {
"abc" => println!("Yes"),
_ => {println!("NO");},
}
enum Option<T> {
Some(T),
None,
}
let opt = Option::Some("Hello");
match opt {
Option::Some(something) => {
println!("{}", something);
},
Option::None => {
println!("opt is nothing");
}
}
let opt: Option<&str> = Option::None;
match opt {
Option::Some(something) => {
println!("{}", something);
},
Option::None => {
println!("opt is nothing");
}
}
fn max(array: &[i32]) -> i32 {
let mut max_index = 0;
let mut i = 1;
while i < array.len() {
if array[i] > array[max_index] {
max_index = i;
}
i += 1;
}
array[max_index]
}
let a = [2, 4, 6, 3, 1];
println!("max = {}", max(&a));
let mut vector = vec![1, 2, 4, 8];
vector.push(16);
vector.push(32);
vector.push(64);
println!("{:?}", vector);
let mut v1: Vec<i32> = vec![1, 2, 4, 8];
let mut v2: Vec<i32> = vec![16, 32, 64];
v1.append(&mut v2);
println!("{:?}", v1);
let v = vec![1, 2, 4, 8];
println!("{}", match v.get(0) {
Some(value) => value.to_string(),
None => "None".to_string()
});
let v = vec![100, 32, 57];
for i in &v {
println!("{}", i);
}
let mut v = vec![100, 32, 57];
for i in &mut v {
*i += 50;
}
let one = 1.to_string();
let float = 1.3.to_string();
let slice = "slice".to_string();
println!("{}-{}-{}", one, float, slice);
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = s1 + &s2;
println!("{}", s3);
let s = String::from("EN中文");
let a = s.chars().nth(2);
println!("{:?}", a);
pub struct ClassName {
field: i32,
}
impl ClassName {
pub fn new(value: i32) -> ClassName {
ClassName {
field: value
}
}
pub fn public_method(&self) {
println!("from public method");
self.private_method();
}
fn private_method(&self) {
println!("from private method");
}
}
let object = ClassName::new(1024);
object.public_method();
println!("{}", object.field);
use std::thread;
use std::time::Duration;
fn spawn_function() {
for i in 0..5 {
println!("spawned thread print {}", i);
thread::sleep(Duration::from_millis(1));
}
}
thread::spawn(spawn_function);
for i in 0..5 {
println!("main thread print {}", i);
thread::sleep(Duration::from_millis(1));
}
let inc = |num: i32| -> i32 {
num + 1
};
println!("inc(5) = {}", inc(5));
thread::spawn(|| {
for i in 0..5 {
println!("spawned thread print {}", i);
thread::sleep(Duration::from_millis(1));
}
});
for i in 0..3 {
println!("main thread print {}", i);
thread::sleep(Duration::from_millis(1));
}
use std::sync::mpsc;
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
});
let received = rx.recv().unwrap();
println!("Got: {}", received);
}