Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 68cce7c

Browse files
'review1'
1 parent 619c8d3 commit 68cce7c

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed

‎examples/enums.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
#[derive(Debug, PartialEq, PartialOrd)]
3+
enum Direction {
4+
Up,
5+
Down,
6+
Left,
7+
Right
8+
}
9+
use Direction::*;
10+
11+
impl Direction {
12+
fn as_str(&self) -> &'static str {
13+
match self {
14+
Up => "up",
15+
Right => "right",
16+
Down => "down",
17+
Left => "left"
18+
}
19+
}
20+
fn next(&self) -> Self {
21+
match *self {
22+
Up => Right,
23+
Right => Down,
24+
Down => Left,
25+
Left => Up
26+
}
27+
}
28+
}
29+
30+
#[derive(Debug)]
31+
enum ValueEnum {
32+
Number(f64),
33+
Str(String),
34+
Bool(bool)
35+
}
36+
37+
use ValueEnum::*;
38+
39+
impl<'a> ValueEnum {
40+
fn to_str(&'a self) -> Option<&'a String> {
41+
match self {
42+
Str(s) => Some(s),
43+
_ => None
44+
}
45+
}
46+
fn to_str1(&self) -> Option<&String> {
47+
if let Str(s) = self {
48+
Some(s)
49+
} else {
50+
None
51+
}
52+
}
53+
}
54+
55+
fn eat_and_dump(x: ValueEnum) {
56+
use ValueEnum::*;
57+
match x {
58+
Number(n) => println!("number is {}", n),
59+
Str(s) => println!("string is {}", s),
60+
Bool(b) => println!("bool is {}", b)
61+
}
62+
}
63+
64+
fn dump(x: &ValueEnum) {
65+
match *x {
66+
Number(n) => println!("number is {}", n),
67+
Str(ref s) => println!("string is {}", s),
68+
Bool(b) => println!("bool is {}", b)
69+
}
70+
}
71+
72+
fn main() {
73+
let mut x = Direction::Left;
74+
assert_eq!(x, Direction::Left);
75+
println!("x as str is {}", x.as_str());
76+
77+
for _ in 0..8 {
78+
println!("x {:?} value is {}", x.next(), x.next() as u32);
79+
if x.next() > Direction::Left {
80+
println!("bigger than Left");
81+
}
82+
x = x.next();
83+
}
84+
85+
use ValueEnum::*;
86+
let n = ValueEnum::Number(2.4);
87+
let s = Str("hello".to_string());
88+
let b = Bool(true);
89+
println!("{:?} {:?} {:?}", n, s, b);
90+
println!("{:?} {:?} {:?}", n, s, b);
91+
dump(&n);
92+
dump(&s);
93+
dump(&b);
94+
95+
println!("s? {:?}", s.to_str());
96+
97+
if n.to_str1() == None {
98+
println!("to str is None");
99+
} else {
100+
println!("s {:?}", n.to_str1());
101+
}
102+
103+
eat_and_dump(n);
104+
eat_and_dump(s);
105+
eat_and_dump(b);
106+
}

‎examples/iterator.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
struct FRange {
3+
val: f64,
4+
end: f64,
5+
incr: f64
6+
}
7+
8+
fn range(x1: f64, x2: f64, skip: f64) -> FRange {
9+
FRange {val: x1, end: x2, incr: skip}
10+
}
11+
12+
impl Iterator for FRange {
13+
type Item = f64;
14+
fn next(&mut self) -> Option<Self::Item> {
15+
let res = self.val;
16+
if res > self.end {
17+
None
18+
} else {
19+
self.val += self.incr;
20+
Some(res)
21+
}
22+
}
23+
}
24+
25+
fn dump<T> (value: &T)
26+
where T: std::fmt::Debug {
27+
println!("value is {:?}", value);
28+
}
29+
30+
fn sqr<T> (x: T) -> T::Output
31+
where T: std::ops::Mul + Copy {
32+
x * x
33+
}
34+
35+
#[derive(Debug)]
36+
struct Foo {
37+
name: String
38+
}
39+
40+
fn main() {
41+
for x in range(0.0, 1.0, 0.1) {
42+
println!("{:1.1}", x);
43+
}
44+
45+
let v: Vec<f64> = range(0.0, 1.0, 0.1).map(|x| x.sin()).collect();
46+
println!("v is {:?}", v);
47+
48+
let x = 10;
49+
dump(&x);
50+
let y = 10.0345;
51+
dump(&y);
52+
53+
let foo = Foo { name: "hello".to_string() };
54+
dump(&foo);
55+
56+
println!("{}", sqr(10));
57+
}

‎examples/matching.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
3+
4+
5+
6+
struct Point {
7+
x: f32,
8+
y: f32
9+
}
10+
11+
fn match_tuple(t: (i32, String)) {
12+
let x = match t {
13+
(0, s) => format!("zero {}", s),
14+
(1, s) if s == "hello" => format!("hello one"),
15+
no_match => format!("No match {:?}", no_match)
16+
};
17+
println!("{}", x);
18+
}
19+
20+
fn main() {
21+
let t = (10, "hello".to_string());
22+
let (ref n, ref s) = t;
23+
println!("n is {}", n);
24+
println!("s is {}", s);
25+
26+
let p = Point { x: 1.0, y: 2.0 };
27+
let Point {x, y} = p;
28+
println!("{} {}", x, y);
29+
30+
match_tuple((1, "wasdf".to_string()));
31+
32+
let ot = Some((2, "hello".to_string()));
33+
if let Some((_, ref s)) = ot {
34+
println!("{}", s);
35+
}
36+
}

‎mykey.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[193,198,108,170,127,209,205,155,63,80,24,196,198,17,201,48,56,136,7,52,92,229,249,144,107,101,49,137,74,98,169,206,140,46,88,67,190,69,160,182,122,26,126,196,32,251,145,234,197,180,129,183,51,88,184,4,74,153,10,103,247,61,154,136]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /