├── .gitignore ├── LICENSE ├── README.md ├── batch ├── update.bat └── update.sh ├── cargo.toml ├── src ├── BillyHerrington_extends_Fucker.rs ├── array.rs ├── cats.rs ├── deconstruction.rs ├── display.rs ├── expect_output.rs ├── float.rs ├── get_variant.rs ├── interception.rs ├── interpretador.rs ├── leer.rs ├── macro_default_value.rs ├── macro_use.rs ├── numero_entero.rs ├── palindrome.rs ├── rain.rs ├── read_clone.rs ├── rightbuttons.rs ├── sametag.rs ├── to_string.rs ├── typeid.rs └── u8.rs └── style └── Rust.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | /target/ 4 | 5 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 6 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock 7 | Cargo.lock 8 | 9 | # These are backup files generated by rustfmt 10 | **/*.rs.bk 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 3442853561 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust-housework 2 | Endless examples for ![Rust](http://latex.codecogs.com/gif.latex?%24%5Ccolor%7Bred%7D%5Ctextbf%7BRust%7D%24) 3 | 4 | 5 | ## Origin 6 | At first there was nothing in this project. There was only endless as well as messy issues. Contributors said, Let there be order: and it was so. And Contributors, looking on the order, saw that it was good: and Contributors made a division between the order and the mess, Naming the order, Answer, and the mess, Issue. 7 | 8 | However, the issue is endless, so the answer is endless. The constant creation of answers makes the contributors feel tired. Then, contributors classify the issues and give examples. And Contributors, looking on the examples, saw that it was good. 9 | 10 | But, the classification of the issues is as endless as housework. So that the contributors are always pull requests not rest until the end of time. 11 | 12 | ## How to use batch 13 | - [Windows](https://github.com/3442853561/rust-housework/blob/master/batch/update.bat) 14 | - [*nix](https://github.com/3442853561/rust-housework/blob/master/batch/update.sh) 15 | -------------------------------------------------------------------------------- /batch/update.bat: -------------------------------------------------------------------------------- 1 | REM This batch will help you update Rust when you network is not "VERY WELL" 2 | SET RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static 3 | SET RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup 4 | TITLE Rust Update 5 | CLS 6 | rustup update 7 | PAUSE 8 | -------------------------------------------------------------------------------- /batch/update.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static 3 | export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup 4 | clear 5 | rustup update 6 | echo 按任意键继续 7 | read -n 1 8 | -------------------------------------------------------------------------------- /cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-housework" 3 | version = "0.0.1" 4 | license = "MIT" 5 | authors = ["3442853561"] 6 | 7 | [[bin]] 8 | name = "BillyHerrington_extends_Fucker" 9 | path = "./src/BillyHerrington_extends_Fucker.rs" 10 | 11 | [[bin]] 12 | name = "array" 13 | path = "./src/array.rs" 14 | 15 | [[bin]] 16 | name = "cats" 17 | path = "./src/cats.rs" 18 | 19 | [[bin]] 20 | name = "deconstruction" 21 | path = "./src/deconstruction.rs" 22 | 23 | [[bin]] 24 | name = "display" 25 | path = "./src/display.rs" 26 | 27 | [[bin]] 28 | name = "expect_output" 29 | path = "./src/expect_output.rs" 30 | 31 | [[bin]] 32 | name = "float" 33 | path = "./src/float.rs" 34 | 35 | [[bin]] 36 | name = "get_variant" 37 | path = "./src/get_variant.rs" 38 | 39 | [[bin]] 40 | name = "interception" 41 | path = "./src/interception.rs" 42 | 43 | [[bin]] 44 | name = "interpretador" 45 | path = "./src/interpretador.rs" 46 | 47 | [[bin]] 48 | name = "leer" 49 | path = "./src/leer.rs" 50 | 51 | [[bin]] 52 | name = "macro_default_value" 53 | path = "./src/macro_default_value.rs" 54 | 55 | [[bin]] 56 | name = "macro_use" 57 | path = "./src/macro_use.rs" 58 | 59 | [[bin]] 60 | name = "numero_entero" 61 | path = "./src/numero_entero.rs" 62 | 63 | [[bin]] 64 | name = "palindrome" 65 | path = "./src/palindrome.rs" 66 | 67 | [[bin]] 68 | name = "rain" 69 | path = "./src/rain.rs" 70 | 71 | [[bin]] 72 | name = "rightbuttons" 73 | path = "./src/rightbuttons.rs" 74 | 75 | [[bin]] 76 | name = "read_clone" 77 | path = "./src/read_clone.rs" 78 | 79 | [[bin]] 80 | name = "sametag" 81 | path = "./src/sametag.rs" 82 | 83 | [[bin]] 84 | name = "to_string" 85 | path = "./src/to_string.rs" 86 | 87 | [[bin]] 88 | name = "typeid" 89 | path = "./src/typeid.rs" 90 | 91 | [[bin]] 92 | name = "u8" 93 | path = "./src/u8.rs" 94 | -------------------------------------------------------------------------------- /src/BillyHerrington_extends_Fucker.rs: -------------------------------------------------------------------------------- 1 | #[derive(Clone)] 2 | struct Fucker { 3 | name: String, 4 | } 5 | 6 | impl Fucker { 7 | fn new>(name: T) -> Self { 8 | Fucker { name: name.into() } 9 | } 10 | 11 | fn get_name(&self) -> String { 12 | self.name.clone() 13 | } 14 | 15 | fn set_name>(&mut self, new_name: T) { 16 | self.name = new_name.into(); 17 | } 18 | } 19 | 20 | struct BillyHerrington { 21 | extends: Fucker, 22 | philosophy: bool, 23 | } 24 | 25 | impl BillyHerrington { 26 | fn new(philosophy: bool) -> Self { 27 | BillyHerrington { 28 | extends: Fucker::new("Billy Herrington"), 29 | philosophy: philosophy, 30 | } 31 | } 32 | 33 | fn get_extends(&self) -> Fucker { 34 | self.extends.clone() 35 | } 36 | 37 | fn set_extends(&mut self, updata_extends: Fucker) { 38 | self.extends = updata_extends; 39 | } 40 | 41 | fn ref_mut_extends<'a>(&'a mut self) -> &'a mut Fucker { 42 | &mut self.extends 43 | } 44 | } 45 | 46 | fn main() { 47 | let mut my_king = BillyHerrington::new(true); 48 | my_king.ref_mut_extends().set_name("King of Fucker"); 49 | println!( 50 | "Billy Herrington is {}", 51 | my_king.ref_mut_extends().get_name() 52 | ); 53 | } 54 | -------------------------------------------------------------------------------- /src/array.rs: -------------------------------------------------------------------------------- 1 | macro_rules! arr { 2 | ($type: ty, $value: expr, $long: expr) => { 3 | Arr::new([$value as $type; $long]) as Arr<$type, [$type; $long]>; 4 | } 5 | } 6 | 7 | macro_rules! gethead { 8 | ($self:ident, $index: expr) => { 9 | $self.head[$index] 10 | } 11 | } 12 | 13 | macro_rules! headlen { 14 | ($self:ident) => { 15 | $self.head.len() 16 | } 17 | } 18 | 19 | macro_rules! get { 20 | ($self:ident, $index: expr) => { 21 | if $index < headlen!($self) { 22 | gethead!($self, $index) 23 | } else if $index < headlen!($self) + $self.follow.len() { 24 | $self.follow[$index - headlen!($self)] 25 | } else { 26 | panic!("Cross-border visit"); 27 | } 28 | } 29 | } 30 | 31 | macro_rules! append { 32 | ($self:ident, $value: expr) => { 33 | $self.follow.push($value); 34 | } 35 | } 36 | 37 | struct Arr { 38 | head: A, 39 | follow: Vec, 40 | } 41 | 42 | impl Arr { 43 | fn new(value: A) -> Self { 44 | Arr { 45 | head: value, 46 | follow: Vec::new(), 47 | } 48 | } 49 | } 50 | 51 | 52 | fn main() { 53 | let mut foo = arr!(u8, 1, 2); 54 | println!("{}", get!(foo, 1)); 55 | append!(foo, 2); 56 | println!("{}", get!(foo, 2)); 57 | println!("{}", get!(foo, 3)); 58 | } 59 | -------------------------------------------------------------------------------- /src/cats.rs: -------------------------------------------------------------------------------- 1 | #[derive(Clone, Debug)] 2 | struct Cat { 3 | fuckable: bool, 4 | } 5 | type Cats = Vec; 6 | 7 | impl Cat { 8 | pub fn fuck(&self) -> Cats { 9 | if self.fuckable { 10 | return vec![ 11 | Cat { fuckable: true }, 12 | Cat { fuckable: false }, 13 | Cat { fuckable: true }, 14 | Cat { fuckable: true }, 15 | Cat { fuckable: false }, 16 | ]; 17 | } 18 | vec![] 19 | } 20 | pub fn cut(&mut self) { 21 | self.fuckable = false; 22 | } 23 | } 24 | 25 | fn main() { 26 | let mut cat_of_fat_worship_guy = Cat { fuckable: true }; 27 | if !cat_of_fat_worship_guy.fuck().is_empty() { 28 | cat_of_fat_worship_guy.cut(); 29 | } 30 | let kids_of_cat_of_fat_worship_guy = cat_of_fat_worship_guy.fuck(); 31 | { 32 | let mut temp = kids_of_cat_of_fat_worship_guy.iter(); 33 | println!("{:?}", temp.next()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/deconstruction.rs: -------------------------------------------------------------------------------- 1 | macro_rules! deconstruction { 2 | ($id:ident, $t:pat, $($p: expr),*) => { 3 | let mut bar = Vec::new(); 4 | for i in $id { 5 | match i { 6 | $t => {$(bar.push($p);)*}, 7 | _ => {}, 8 | } 9 | } 10 | let mut $id = bar.clone(); 11 | } 12 | } 13 | 14 | fn main() { 15 | let foo = vec![(1,((1,2),(3,4))),(1,((1,2),(3,4)))]; 16 | deconstruction!(foo,(e,((f,g),(h,j))),e,f,g,h,j); 17 | for i in foo { 18 | print!("{} ", i); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/display.rs: -------------------------------------------------------------------------------- 1 | use std::fmt; 2 | 3 | struct Example(f32, f32, f32, f32); 4 | 5 | impl fmt::Display for Example { 6 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 7 | write!(f, "({}, {}, {}, {})", self.0, self.1, self.2, self.3) 8 | } 9 | } 10 | 11 | fn main() { 12 | let foo = Example(0.0, 0.1, 0.2, 0.3); 13 | println!("{}", foo); 14 | } 15 | -------------------------------------------------------------------------------- /src/expect_output.rs: -------------------------------------------------------------------------------- 1 | // fn main() { 2 | // let v: [u8; 4] = [2, 3, 4, 42]; 3 | // let mut it = v.iter().filter(|a| **a > 3); 4 | // 5 | // let s = it.by_ref().fold(0, |acc, i| acc + i); 6 | // println!("{:?}", s); 7 | // 8 | // let s: Vec<&u8> = it.collect(); 9 | // // I expect it output [4, 42] 10 | // println!("{:?}", s); 11 | // } 12 | 13 | fn main() { 14 | let v: [u8; 4] = [2, 3, 4, 42]; 15 | let mut it = v.iter().filter(|a| **a > 3); 16 | 17 | let s: Vec<&u8> = it.by_ref().collect(); 18 | let txt = format!("{:?}", s); 19 | let s = s.iter().fold(0u8, |acc, i| acc + **i); 20 | println!("{:?}", s); 21 | println!("{}", txt); 22 | } 23 | -------------------------------------------------------------------------------- /src/float.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut sum = 0; 3 | let bit_exp: u32 = 4; 4 | let bit_frac: u32 = 3; 5 | for exp in 0..2u64.pow(bit_exp) - 1 { 6 | for frac in 0..2u64.pow(bit_frac) { 7 | let mut e: f64 = exp as f64; 8 | let mut m: f64 = 1.0; 9 | if exp == 0 { 10 | e = 1.0; 11 | m = 0.0; 12 | } 13 | e -= 2.0f64.powf(bit_exp as f64 - 1.0) - 1.0; 14 | let mut th = frac; 15 | for i in 0..bit_frac { 16 | m += (th % 2) as f64 * 2.0f64.powf(i as f64 - bit_frac as f64); 17 | th /= 2; 18 | } 19 | let foo: f64 = m * 2.0f64.powf(e); 20 | println!("{}", foo); 21 | if foo < 1.0 { 22 | sum += 1; 23 | } 24 | } 25 | } 26 | println!("{}", sum); 27 | } 28 | -------------------------------------------------------------------------------- /src/get_variant.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Debug; 2 | 3 | #[derive(PartialEq, Eq)] 4 | struct Variant { 5 | variant: String, 6 | } 7 | 8 | trait GetVariant where Self: Debug { 9 | fn get_variant(&self) -> Variant { 10 | let this_variant = format!("{:?}", self); 11 | let mut result = "".to_string(); 12 | for i in this_variant.chars() { 13 | if i == '(' { 14 | break; 15 | } 16 | result.push(i); 17 | } 18 | Variant { 19 | variant: result, 20 | } 21 | } 22 | } 23 | 24 | impl GetVariant for Option {} 25 | 26 | fn main() { 27 | println!("{:?}", Some(2)); 28 | println!("{}", Some(1).get_variant() == Some(2).get_variant() ); 29 | } 30 | -------------------------------------------------------------------------------- /src/interception.rs: -------------------------------------------------------------------------------- 1 | use std::convert::AsMut; 2 | trait Trait { 3 | fn take(&mut self, start: usize, end: usize); 4 | } 5 | 6 | impl Trait for Vec { 7 | fn take(&mut self, start: usize, end: usize){ 8 | let cut = self.len() - end - 1; 9 | for _ in 0..cut { 10 | self.pop(); 11 | } 12 | let mut foo: Self = Vec::new(); 13 | for _ in start..end + 1 { 14 | foo.push(self.pop().unwrap()); 15 | } 16 | *self = Vec::new(); 17 | for _ in 0..foo.len() { 18 | self.push(foo.pop().unwrap()); 19 | } 20 | } 21 | } 22 | 23 | fn avaricious(foo: &mut Vec, start_token: T, end_token: T) 24 | where T: PartialEq { 25 | let mut start = 0; 26 | loop { 27 | if foo[start] == start_token { 28 | break; 29 | } 30 | start += 1; 31 | } 32 | let mut end = foo.len() - 1; 33 | loop { 34 | if foo[end] == end_token { 35 | break; 36 | } 37 | end -= 1; 38 | } 39 | 40 | foo.take(start, end); 41 | } 42 | 43 | fn header(foo: &mut Vec, start_token: T, end_token: T) 44 | where T: PartialEq { 45 | let mut start = 0; 46 | loop { 47 | if foo[start] == start_token { 48 | break; 49 | } 50 | start += 1; 51 | } 52 | let mut end = start + 1; 53 | loop { 54 | if foo[end] == end_token { 55 | break; 56 | } 57 | end += 1; 58 | } 59 | 60 | foo.take(start, end); 61 | } 62 | 63 | fn footer(foo: &mut Vec, start_token: T, end_token: T) 64 | where T: PartialEq { 65 | let mut end = foo.len() - 1; 66 | loop { 67 | if foo[end] == end_token { 68 | break; 69 | } 70 | end -= 1; 71 | } 72 | let mut start = end - 1; 73 | loop { 74 | if foo[start] == start_token { 75 | break; 76 | } 77 | start -= 1; 78 | } 79 | foo.take(start, end); 80 | } 81 | 82 | fn main() { 83 | let mut foo: Vec = vec![0, 2, 1, 0, 3, 1]; 84 | let mut bar: Vec = vec![0, 2, 1, 0, 3, 1]; 85 | avaricious(foo.as_mut(), 0, 1); 86 | println!("{:?}", foo); 87 | header(foo.as_mut(), 0, 1); 88 | println!("{:?}", foo); 89 | footer(bar.as_mut(), 0, 1); 90 | println!("{:?}", bar); 91 | } -------------------------------------------------------------------------------- /src/interpretador.rs: -------------------------------------------------------------------------------- 1 | extern crate regex; 2 | 3 | fn get_base(s: &str) -> (u128, u32) { 4 | use regex::Regex; 5 | if Regex::new(r"\.").unwrap().is_match(format!("{}", s).trim()) { 6 | ( 7 | Regex::new(r"(?P\d*)\.(?P\d*)") 8 | .unwrap() 9 | .replace_all(format!("{}", s).trim(), "$a$b") 10 | .parse() 11 | .unwrap(), 12 | format!("{}", s).trim().len() as u32 13 | - Regex::new(r"\.") 14 | .unwrap() 15 | .find(format!("{}", s).trim()) 16 | .unwrap() 17 | .start() as u32 18 | - 1, 19 | ) 20 | } else { 21 | (format!("{}", s).trim().parse().unwrap(), 0) 22 | } 23 | } 24 | 25 | fn str_to_u32(s: &str) -> u32 { 26 | format!("{}", s).trim().parse().unwrap() 27 | } 28 | 29 | fn to_u128(s: String) -> u128 { 30 | use regex::Regex; 31 | let m = Regex::new(r"u128e").unwrap().find(&*s).unwrap(); 32 | let (base, unpow) = get_base(&s[0..m.start()]); 33 | base * 10u128.pow(str_to_u32(&s[m.end()..])) / 10u128.pow(unpow) 34 | } 35 | 36 | macro_rules! numero_entero { 37 | (let $numero:ident = $texto:tt) => { 38 | let $numero = to_u128(stringify!($texto).to_string()); 39 | }; 40 | (let mut $numero:ident = $texto:tt) => { 41 | let mut $numero = to_u128(stringify!($texto).to_string()); 42 | }; 43 | ($numero:ident = $texto:tt) => { 44 | $numero = to_u128(stringify!($texto).to_string()); 45 | }; 46 | } 47 | 48 | fn main() { 49 | numero_entero!(let foo = 1.1u128e18); 50 | println!("{}", foo); 51 | numero_entero!(let mut bar = 2u128e18); 52 | println!("{}", bar); 53 | numero_entero!(bar = 3.2u128e5); 54 | println!("{}", bar); 55 | } 56 | -------------------------------------------------------------------------------- /src/leer.rs: -------------------------------------------------------------------------------- 1 | trait Inputable { 2 | fn input(arg: impl Into) -> Self; 3 | } 4 | 5 | impl Inputable for i32 { 6 | fn input(arg: impl Into) -> Self { 7 | arg.into().trim().parse().unwrap() 8 | } 9 | } 10 | 11 | macro_rules! leer_teclado { 12 | ($arg: tt: $type: ty) => { 13 | let mut input = String::new(); 14 | let _ = std::io::stdin().read_line(&mut input); 15 | let $arg: $type = Inputable::input(input); 16 | }; 17 | ($arg: tt) => { 18 | let mut input = String::new(); 19 | let _ = std::io::stdin().read_line(&mut input); 20 | let $arg = input; 21 | }; 22 | } 23 | 24 | fn main() { 25 | leer_teclado!(a); 26 | println!("{}", a); 27 | leer_teclado!(a: i32); 28 | println!("{}", a); 29 | } 30 | -------------------------------------------------------------------------------- /src/macro_default_value.rs: -------------------------------------------------------------------------------- 1 | // One parameter of this macro has a default value 2 | macro_rules! make_a_awesome_function { 3 | ($id: ident) => { 4 | fn $id() { 5 | println!("You called a awesome function named {}!", stringify!($id)); 6 | } 7 | }; 8 | () => { 9 | make_a_awesome_function!(foo); 10 | }; 11 | } 12 | 13 | make_a_awesome_function!(bar); 14 | make_a_awesome_function!(); 15 | 16 | fn main() { 17 | foo(); 18 | bar(); 19 | } 20 | -------------------------------------------------------------------------------- /src/macro_use.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | mod bar { 3 | macro_rules! macro0 { () => (()) } 4 | } 5 | 6 | 7 | mod foo { 8 | macro_rules! macro1 { () => (()) } 9 | } 10 | 11 | fn main() { 12 | macro0!(); 13 | // macro1! 是不能用的 14 | } 15 | -------------------------------------------------------------------------------- /src/numero_entero.rs: -------------------------------------------------------------------------------- 1 | macro_rules! numero_entero { 2 | ($numero:tt u128e $potenciacion:tt) => { 3 | $numero * 10u128.pow($potenciacion) 4 | } 5 | } 6 | 7 | #[inline] 8 | fn numero(num: u128, pot: u32) -> u128 { 9 | // Me gusta 10 | num * 10u128.pow(pot) 11 | } 12 | 13 | fn main() { 14 | let foo = numero_entero!(1 u128e 18); 15 | println!("{}", foo); 16 | println!("{}", numero(1, 18)); 17 | } 18 | -------------------------------------------------------------------------------- /src/palindrome.rs: -------------------------------------------------------------------------------- 1 | trait Trait { 2 | fn to_letters(&self) -> String; 3 | fn palindrome(&self) -> bool; 4 | fn lazy_output(&self); 5 | 6 | fn mid(&self) -> Option ; 7 | fn lazy_mid(&self) -> char; 8 | } 9 | 10 | impl Trait for str { 11 | fn to_letters(&self) -> String { 12 | let mut bar:Vec = Vec::new(); 13 | for ch in self.chars() { 14 | match ch { 15 | 'a'...'z' => bar.push(ch as u8), 16 | 'A'...'Z' => bar.push(ch as u8 + 97 - 65), 17 | _ => {}, 18 | } 19 | } 20 | String::from_utf8(bar).unwrap() 21 | } 22 | 23 | fn palindrome(&self) -> bool { 24 | let mut i = 0; 25 | while i < self.len() - i - 1 { 26 | if self.chars().nth(i) != self.chars().nth(self.len() - i - 1) { 27 | return false; 28 | } 29 | i += 1; 30 | } 31 | true 32 | } 33 | 34 | fn lazy_output(&self) { 35 | if self.to_letters().palindrome() { 36 | println!("{:?} is palindrome.", self); 37 | } else { 38 | println!("{:?} is not palindrome.", self); 39 | } 40 | } 41 | 42 | fn mid(&self) -> Option { 43 | if self.len() % 2 == 0 { 44 | None 45 | } else { 46 | self.chars().nth(self.len()/2) 47 | } 48 | } 49 | 50 | fn lazy_mid(&self) -> char { 51 | self.to_letters().mid().unwrap() 52 | } 53 | } 54 | 55 | fn main() { 56 | let foo = "A man, a plan, a cat, a canal, Panama."; 57 | foo.lazy_output(); 58 | println!("{:?}", foo.lazy_mid()); 59 | } -------------------------------------------------------------------------------- /src/rain.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let foo = [2, 5, 1, 2, 3, 4, 7, 7, 6]; 3 | let mut left = 0; 4 | let mut right = foo.len() - 1; 5 | let mut i = 0; 6 | let mut lim = 0; 7 | let mut sum = 0; 8 | while i < foo.len() { 9 | if foo[left] > foo[i] { 10 | break; 11 | } else { 12 | left = i; 13 | } 14 | i += 1; 15 | } 16 | i = foo.len() - 1; 17 | while i > 0 { 18 | if foo[right] > foo[i] { 19 | break; 20 | } else { 21 | right = i; 22 | } 23 | i -= 1; 24 | } 25 | if foo[left] < foo[right] { 26 | lim = foo[left]; 27 | } else { 28 | lim = foo[right]; 29 | } 30 | for i in left..right + 1 { 31 | if foo[i] < lim { 32 | sum += lim - foo[i]; 33 | } 34 | } 35 | println!("{}", sum); 36 | } 37 | -------------------------------------------------------------------------------- /src/read_clone.rs: -------------------------------------------------------------------------------- 1 | fn read_clone(read: &mut Vec, start: usize, end: usize) -> Vec 2 | where T: Clone { 3 | let mut el_read: Vec = Vec::new(); 4 | let mut el: Option = None; 5 | let mut foo: Vec = Vec::new(); 6 | for _ in 0..read.len() - start { 7 | el_read.push(read.pop().unwrap()); 8 | } 9 | for _ in start..end + 1 { 10 | el = el_read.pop(); 11 | foo.push(el.clone().unwrap()); 12 | read.push(el.clone().unwrap()); 13 | } 14 | for _ in 0..el_read.len() { 15 | read.push(el_read.pop().unwrap()); 16 | } 17 | foo 18 | } 19 | 20 | fn lazy_output(read:&Vec) 21 | where T: std::fmt::Display { 22 | for i in read { 23 | print!("{} ", i); 24 | } 25 | println!(""); 26 | } 27 | 28 | fn main() { 29 | let mut foo = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 30 | let bar = read_clone(&mut foo, 1, 3); 31 | lazy_output(&foo); 32 | lazy_output(&bar); 33 | } -------------------------------------------------------------------------------- /src/rightbuttons.rs: -------------------------------------------------------------------------------- 1 | use std::fs::File; 2 | use std::io::prelude::*; 3 | use std::process::Command; 4 | use std::io; 5 | 6 | fn read_from_stdin(buf: &mut String) -> io::Result<()> { 7 | try!(io::stdin().read_line(buf)); 8 | Ok(()) 9 | } 10 | 11 | fn bar(input: &mut String) -> std::io::Result<()> { 12 | let foo = include_str!(file!{});; 13 | let mut file = File::create(input)?; 14 | file.write_all(foo.as_bytes())?; 15 | Ok(()) 16 | } 17 | 18 | fn main() { 19 | loop { 20 | let mut input = String::new(); 21 | read_from_stdin(&mut input); 22 | if input.trim() == "#".to_string() { 23 | break; 24 | } 25 | input = input.trim().to_string() + ".rs"; 26 | let temp = "rustc ".to_string() + &*input; 27 | let _ = bar(&mut input); 28 | let output = if cfg!(target_os = "windows") { 29 | Command::new("cmd") 30 | .args(&["/C", &*temp]) 31 | .output() 32 | .expect("failed to execute process") 33 | } else { 34 | Command::new("sh") 35 | .arg("-c") 36 | .arg(&*temp) 37 | .output() 38 | .expect("failed to execute process") 39 | }; 40 | println!("You know, boys, a nuclear reactor is a lot like a woman. You just have to read the manual and press the right buttons. (by Homer Simpson)"); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/sametag.rs: -------------------------------------------------------------------------------- 1 | trait SameTag { 2 | fn same_tag(&self, other: &Self) -> bool; 3 | } 4 | 5 | impl SameTag for Option { 6 | #[allow(unused_assignments)] 7 | fn same_tag(&self, other: &Self) -> bool { 8 | let mut self_tag = 0; 9 | let mut other_tag = 0; 10 | match *self { 11 | Some(_) => self_tag = 0, 12 | None => self_tag = 1, 13 | } 14 | match *other { 15 | Some(_) => other_tag = 0, 16 | None => other_tag = 1, 17 | } 18 | self_tag == other_tag 19 | } 20 | } 21 | 22 | fn main() { 23 | println!("{}", Some(1).same_tag(&Some(2))); 24 | } 25 | 26 | // use std::mem; 27 | // enum Foo { A(&'static str), B(i32), C(i32) } 28 | // assert!(mem::discriminant(&Foo::A("bar")) == mem::discriminant(&Foo::A("baz"))); 29 | // assert!(mem::discriminant(&Foo::B(1)) == mem::discriminant(&Foo::B(2))); 30 | // assert!(mem::discriminant(&Foo::B(3)) != mem::discriminant(&Foo::C(3))); 31 | -------------------------------------------------------------------------------- /src/to_string.rs: -------------------------------------------------------------------------------- 1 | fn to_string>(num: T) -> String { 2 | format!("{}", num.into()) 3 | } 4 | 5 | fn main() { 6 | let foo = &*to_string(0); 7 | println!("{}", foo); 8 | } 9 | -------------------------------------------------------------------------------- /src/typeid.rs: -------------------------------------------------------------------------------- 1 | use std::any::TypeId; 2 | fn string() -> TypeId { 3 | TypeId::of::() 4 | } 5 | 6 | fn foo(para_type: TypeId) { 7 | if TypeId::of::() == para_type { 8 | println!("参数为类型 String"); 9 | } 10 | } 11 | 12 | fn main() { 13 | foo(string()); 14 | } 15 | -------------------------------------------------------------------------------- /src/u8.rs: -------------------------------------------------------------------------------- 1 | trait ToU8 { 2 | fn to_u8(self) -> T; 3 | } 4 | 5 | impl ToU8<(u8, u8)> for u16 { 6 | fn to_u8(self) -> (u8, u8) { 7 | ((self >> 8) as u8, 8 | self as u8) 9 | } 10 | } 11 | 12 | impl ToU8<(u8, u8, u8, u8)> for u32 { 13 | fn to_u8(self) -> (u8, u8, u8, u8) { 14 | ((self >> 24) as u8, 15 | (self >> 16) as u8, 16 | (self >> 8) as u8, 17 | self as u8) 18 | } 19 | } 20 | 21 | impl ToU8<(u8, u8, u8, u8, u8, u8, u8, u8)> for u64 { 22 | fn to_u8(self) -> (u8, u8, u8, u8, u8, u8, u8, u8) { 23 | ((self >> 56) as u8, 24 | (self >> 48) as u8, 25 | (self >> 40) as u8, 26 | (self >> 32) as u8, 27 | (self >> 24) as u8, 28 | (self >> 16) as u8, 29 | (self >> 8) as u8, 30 | self as u8) 31 | } 32 | } 33 | 34 | 35 | fn main() { 36 | let foo: u64 = 256 * 256 * 256; 37 | println!("{:?}", foo.to_u8()); 38 | } 39 | -------------------------------------------------------------------------------- /style/Rust.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 00// 01 02 03/* 04*/ 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | ( ) [ ] ... . , ^ % > < ! = + - | : => >> << ; :: 17 | 18 | { 19 | 20 | } 21 | 22 | 23 | 24 | 25 | 26 | 27 | as break continue do else false fn for if impl in let loop match mod return self super true trait type unsafe use while move mut static extern struct enum ref box const priv pub 28 | f16 f32 f64 isize i8 i16 i32 i64 usize u8 u16 u32 u64 bool str String 29 | * & ' $ &mut 30 | 31 | 32 | 33 | 34 | 35 | 00" 01\ 02" 03#[ 04 05] 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | --------------------------------------------------------------------------------