Skip to content

Commit 7c377b9

Browse files
committed
Changes to version 1.3.0 (9a92aaf19 2015-09-15)
1 parent 2075c39 commit 7c377b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+921
-728
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
*~
22
/build/
3+
Cargo.lock
34
/doc/
5+
/inifile/target/
6+
/inifile/src/data/save_test.ini
7+
/inifile/src/data/write_test.ini
48
/lib/
59
/lib-stamps/
610
/nbproject/private/
11+
*.o
12+
/target/

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: rust
2-
rust: 0.12.0
2+
rust: 1.4.0
33
env:
44
global:
55
- secure: YuWDOwPGprL6PiBZVeGaLOCHHgC18fQ6nq617tlt7XFCkG17r/xDWq3iiAyNxNUcwsD9CkWi5aXok8SlX3rQx84XC/sya2bi0+8Frt9EztcVxgMwWuX3Ll4mFHO7HnMhazoQYRPd0b0w4s7bFY2WidxCqjsMKRgAM26Gn+6oQto=

Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,26 @@
33
name = "rust-examples"
44
version = "0.0.1"
55
authors = ["Eliovir <[email protected]>"]
6+
7+
[dependencies.inifile]
8+
path = "inifile"
9+
10+
[dependencies]
11+
getopts = "0.2.14"
12+
rand = "0.3"
13+
14+
[[bin]]
15+
name = "api-getopts"
16+
path = "api-getopts.rs"
17+
18+
[[bin]]
19+
name = "book-3_1-guessing_game"
20+
path = "book-3_1-guessing_game.rs"
21+
22+
[[bin]]
23+
name = "tutorial-tasks-02_3-arc"
24+
path = "tutorial-tasks-02_3-arc.rs"
25+
26+
[[bin]]
27+
name = "tutorial-04_2-pattern-matching"
28+
path = "tutorial-04_2-pattern-matching.rs"

Makefile

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
CARGO=printf "\033[32;1mCargo:\033[33m %s\033[m\n" $@; cargo
12
RUSTC=printf "\033[32;1mRustc:\033[33m %s\033[m\n" $@; rustc
23
LIBSRC:=$(shell grep -l 'crate_type = "lib"' *rs)
34
LIBSTAMP=$(patsubst %.rs,lib-stamps/%,$(LIBSRC))
@@ -20,7 +21,7 @@ help:
2021

2122
clean:
2223
# Remove executables, test files, libraries
23-
rm -fr $(PROG) $(TESTPROG) *~ build/ doc/ lib/ lib-stamps/
24+
rm -fr $(PROG) $(TESTPROG) *.o *~ build/ doc/ lib/ lib-stamps/ target/
2425

2526
bench: $(TESTPROG)
2627
# Run benchmarks
@@ -60,9 +61,32 @@ version:
6061
# Display version of source code
6162
git describe
6263

64+
build/api-getopts: api-getopts.rs build
65+
$(CARGO) build --bin api-getopts
66+
cp target/debug/api-getopts $@
67+
68+
build/book-3_1-guessing_game: book-3_1-guessing_game.rs build
69+
$(CARGO) build --bin book-3_1-guessing_game
70+
cp target/debug/book-3_1-guessing_game $@
71+
72+
build/inifile: inifile.rs build
73+
$(CARGO) build --bin inifile
74+
cp target/debug/inifile $@
75+
6376
build/tutorial-tasks-02_2-backgrounding_computations: tutorial-tasks-02_2-backgrounding_computations.rs $(LIBSTAMP) build
6477
$(RUSTC) $(RUSTFLAGS) $< -o $@ -L lib/
6578

79+
build/tutorial-tasks-02_3-arc: tutorial-tasks-02_3-arc.rs build
80+
$(CARGO) build --bin tutorial-tasks-02_3-arc
81+
cp target/debug/tutorial-tasks-02_3-arc $@
82+
83+
build/tutorial-04_2-pattern-matching: tutorial-04_2-pattern-matching.rs build
84+
$(CARGO) build --bin tutorial-04_2-pattern-matching
85+
cp target/debug/tutorial-04_2-pattern-matching $@
86+
87+
build:
88+
mkdir -p build
89+
6690
build/% : %.rs
6791
mkdir -p build
6892
$(RUSTC) $(RUSTFLAGS) $< -o $@
@@ -73,6 +97,11 @@ lib-stamps/% : %.rs
7397
mkdir -p lib-stamps ;
7498
$(RUSTC) --out-dir lib/ $< > $@;
7599

100+
lib-stamps/inifilex : inifile.rs
101+
mkdir -p lib
102+
mkdir -p lib-stamps ;
103+
$(CARGO) build --lib inifile
104+
76105
build/test-% : %.rs $(LIBSTAMP)
77106
mkdir -p build
78107
$(RUSTC) $(RUSTFLAGS) $< -o $@ --test -L lib/

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ Examples are tested with version 0.12.0.
2424
* [15](http://doc.rust-lang.org/tutorial.html#closures) Closures: `tutorial-15-closure.rs`
2525
* [16](http://doc.rust-lang.org/tutorial.html#methods) Methods, with *constructor*: `tutorial-16-methods.rs`
2626
* [17](http://doc.rust-lang.org/tutorial.html#generics) Generics: `tutorial-17-generics.rs`
27-
* [Rust Tasks and Communication]
28-
* [2](http://doc.rust-lang.org/guide-tasks.html#basics) Basics: `tutorial-tasks-02-basics.rs`
27+
* [Book]
28+
* [4.6](http://doc.rust-lang.org/book/concurrency.html#threads) Concurrency, threads: `book-4-6-threads.rs`
2929
* [2.1](http://doc.rust-lang.org/guide-tasks.html#communication) Communication: `tutorial-tasks-02_1-communication.rs`
30-
* [2.2](http://doc.rust-lang.org/guide-tasks.html#backgrounding-computations:-futures) Backgrounding computations: Futures: `tutorial-tasks-02_2-backgrounding_computations.rs`
3130
* [2.3](http://doc.rust-lang.org/guide-tasks.html#sharing-immutable-data-without-copy:-arc) Sharing immutable data without copy: Arc: `tutorial-tasks-02_3-arc.rs`
3231
* [Doc unit testing]
3332
* Unit testing in Rust: `unittests.rs`
@@ -42,7 +41,7 @@ Examples are tested with version 0.12.0.
4241
* [collections:hashmap::HashMap](http://doc.rust-lang.org/collections/hashmap/struct.HashMap.html): `api-collections-hashmap.rs`
4342
* [getopts](http://doc.rust-lang.org/getopts/index.html): `api-getopts.rs`
4443
* [std::from_str::FromStr](http://doc.rust-lang.org/std/from_str/trait.FromStr.html): `api-std-from_str.rs`
45-
* [std::io::File](http://doc.rust-lang.org/std/io/index.html): `api-std-io-file.rs`
44+
* [std::fs::File](http://doc.rust-lang.org/std/fs/index.html): `api-std-fs-file.rs`
4645
* [std::vec](http://doc.rust-lang.org/std/vec/index.html): OwnedVector, 2D-arrays, ...: `api-std-vec.rs`
4746
* Some new files:
4847
* `Makefile` to compile, run tests and run benchmarks
@@ -61,7 +60,7 @@ Examples are tested with version 0.12.0.
6160

6261
[Tutorial]: http://doc.rust-lang.org/tutorial.html
6362
[The Rust Reference Manual]: http://doc.rust-lang.org/rust.html
64-
[Rust Tasks and Communication]: http://doc.rust-lang.org/guide-tasks.html
63+
[Book]: http://doc.rust-lang.org/book/
6564
[Doc unit testing]: http://doc.rust-lang.org/guide-testing.html
6665
[Rust Cheatsheet]: http://doc.rust-lang.org/complement-cheatsheet.html#how-do-i-express-phantom-types?
6766

api-collections-hashmap.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
/**
2-
* http://doc.rust-lang.org/std/collections/hashmap/struct.HashMap.html
3-
* https://github.com/rust-lang/rust/blob/master/src/test/run-pass/hashmap-memory.rs#L39
4-
* https://github.com/rust-lang/rust/blob/master/src/libstd/collections/hashmap.rs
2+
* http://doc.rust-lang.org/stable/std/collections/struct.HashMap.html
3+
* https://github.com/rust-lang/rust/blob/1.0.0/src/test/run-pass/hashmap-memory.rs
4+
* https://github.com/rust-lang/rust/blob/1.0.0/src/libstd/collections/hash/map.rs
55
*
66
* @license MIT license <http://www.opensource.org/licenses/mit-license.php>
77
*/
8-
extern crate debug;
9-
use std::collections::hashmap::{HashMap, Occupied, Vacant};
8+
use std::collections::HashMap;
9+
use std::collections::hash_map::Entry::{Occupied, Vacant};
1010

1111
fn main() {
1212
println!("Using borrowed pointers as keys.");
13-
let mut h: HashMap<&str, int>;
13+
let mut h: HashMap<&str, isize>;
1414
h = HashMap::new();
1515
h.insert("foo", 42);
1616
println!("Is there a key foo? => {}", h.contains_key(&("foo"))); // => true
1717
println!("Is there a key baz? => {}", h.contains_key(&("baz"))); // => false
18-
println!("The value for foo is => {:?}", h.find(&("foo"))); // => Some(&42)
18+
println!("The value for foo is => {:?}", h.get(&("foo"))); // => Some(&42)
1919
let key = "baz";
2020
h.insert(key, 1);
2121
println!("Is there a key baz? => {}", h.contains_key(&("baz"))); // => false
2222

2323
// Doing a find, inserting with a `proc()`, using the key to construct the value
2424
let mut map = HashMap::<String, String>::new();
2525
match map.entry("foo".to_string()) {
26-
Vacant(entry) => { entry.set("bar".to_string()); },
26+
Vacant(entry) => { entry.insert("bar".to_string()); },
2727
Occupied(mut entry) => { entry.get_mut().push_str("bar"); },
2828
}
29-
println!("The value for foo is => {:?}", map.find(&("foo".to_string()))); // => Some(&~"foobar")
29+
println!("The value for foo is => {:?}", map.get(&("foo".to_string()))); // => Some(&~"foobar")
3030
// running this for the first time, will add "foo" with the value 1
3131
// running the same for the second time, will add +1 to "foo"
3232
match h.entry("foo") {
33-
Vacant(entry) => { entry.set(1); },
33+
Vacant(entry) => { entry.insert(1); },
3434
Occupied(mut entry) => { *entry.get_mut() += 1; },
3535
}
36-
println!("foo={}", h.get(&("foo")));
36+
println!("foo={:?}", h.get(&("foo")));
3737
assert_eq!(h["foo"], 43);
3838

3939
// You don't actually need the HashMap to own the keys (but
4040
// unless all keys are static, this will be likely to lead
4141
// to problems, so I don't suggest you do it in reality)
4242

4343
println!("Using owned pointers as keys.");
44-
let mut h = HashMap::<String, int>::new();
44+
let mut h = HashMap::<String, isize>::new();
4545
h.insert("foo".to_string(), 42);
4646
println!("Is there a key foo? => {}", h.contains_key(&"foo".to_string())); // => true
4747
println!("Is there a key baz? => {}", h.contains_key(&"baz".to_string())); // => false
48-
println!("The value for foo is => {:?}", h.find(&"foo".to_string())); // => Some(&42)
48+
println!("The value for foo is => {:?}", h.get(&"foo".to_string())); // => Some(&42)
4949
h.insert(key.to_string(), 1);
5050
println!("Is there a key baz? => {}", h.contains_key(&"baz".to_string())); // => true
5151

@@ -54,13 +54,13 @@ fn main() {
5454
for (k, _) in h.iter() {
5555
keys.push(k.to_string());
5656
}
57-
println!("These are the keys: {}.", keys);
57+
println!("These are the keys: {:?}.", keys);
5858
let keys = h.keys().map(|v| v.clone()).collect::<Vec<String>>();
59-
println!("These are the keys: {}.", keys);
59+
println!("These are the keys: {:?}.", keys);
6060

6161
// List values of the HashMap
62-
let values = h.values().map(|v| v.clone()).collect::<Vec<int>>();
63-
println!("These are the values: {}.", values);
62+
let values = h.values().map(|v| v.clone()).collect::<Vec<isize>>();
63+
println!("These are the values: {:?}.", values);
6464

6565
// type inference lets us omit an explicit type signature (which
6666
// would be `HashMap<&str, &str>` in this example).
@@ -84,7 +84,7 @@ fn main() {
8484
// look up the values associated with some keys.
8585
let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
8686
for book in to_find.iter() {
87-
match book_reviews.find(book) {
87+
match book_reviews.get(book) {
8888
Some(review) => println!("{}: {}", *book, *review),
8989
None => println!("{} is unreviewed.", *book)
9090
}

api-getopts.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
2-
* http://doc.rust-lang.org/getopts/index.html
2+
* http://doc.rust-lang.org/getopts/getopts/index.html
33
*
44
* @license MIT license <http://www.opensource.org/licenses/mit-license.php>
55
*/
66
extern crate getopts;
7-
use getopts::{optopt,optflag,getopts,OptGroup};
8-
use std::os;
7+
use getopts::Options;
8+
use std::env;
99

1010
fn do_work(inp: &str, out: Option<String>) {
1111
println!("{}", inp);
@@ -15,35 +15,35 @@ fn do_work(inp: &str, out: Option<String>) {
1515
}
1616
}
1717

18-
fn print_usage(program: &str, _opts: &[OptGroup]) {
19-
println!("Usage: {} [options]", program);
20-
println!("-o\t\tOutput");
21-
println!("-h --help\tUsage");
18+
fn print_usage(program: &str, opts: Options) {
19+
let brief = format!("Usage: {} FILE [options]", program);
20+
print!("{}", opts.usage(&brief));
2221
}
2322

23+
#[cfg(not(test))]
2424
fn main() {
25-
let args: Vec<String> = os::args();
25+
let args: Vec<String> = env::args().collect();
2626

2727
let program = args[0].clone();
2828

29-
let opts = [
30-
optopt("o", "", "set output file name", "NAME"),
31-
optflag("h", "help", "print this help menu")
32-
];
33-
let matches = match getopts(args.tail(), opts) {
29+
let mut opts = Options::new();
30+
opts.optopt("o", "", "set output file name", "NAME");
31+
opts.optflag("h", "help", "print this help menu");
32+
33+
let matches = match opts.parse(&args[1..]) {
3434
Ok(m) => { m }
35-
Err(f) => { fail!(f.to_string()) }
35+
Err(f) => { panic!(f.to_string()) }
3636
};
3737
if matches.opt_present("h") {
38-
print_usage(program.as_slice(), opts);
38+
print_usage(&program, opts);
3939
return;
4040
}
4141
let output = matches.opt_str("o");
4242
let input = if !matches.free.is_empty() {
4343
matches.free[0].clone()
4444
} else {
45-
print_usage(program.as_slice(), opts);
45+
print_usage(&program, opts);
4646
return;
4747
};
48-
do_work(input.as_slice(), output);
48+
do_work(&input, output);
4949
}

api-std-from_str.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
/**
22
* Converting from String
33
* http://doc.rust-lang.org/std/from_str/trait.FromStr.html
4+
* http://doc.rust-lang.org/std/primitive.str.html#method.parse
45
*
56
* @license MIT license <http://www.opensource.org/licenses/mit-license.php>
67
**/
8+
use std::num::ParseIntError;
79

810
fn main() {
911
/*
1012
* When converting from string to numbers, you will have to provide the type manually
1113
*/
12-
let f: f32 = from_str("1.2").unwrap();
14+
let f: f32 = "1.2".parse().ok().expect("Wrong format!");
1315
assert_eq!(f, 1.2f32);
1416
// or
15-
let i = from_str::<uint>("5").unwrap();
17+
let i : i32 = "5".parse().unwrap();
1618
assert_eq!(i, 5);
1719

18-
let oi: Option<uint> = from_str("1");
19-
assert_eq!(oi, Some(1u));
20-
// None, if the provided string cannot be converted
21-
let oi: Option<uint> = from_str("x");
22-
assert_eq!(oi, None);
20+
let oi: Result<u32, ParseIntError> = "1".parse();
21+
assert_eq!(oi, Ok(1));
22+
// Error, if the provided string cannot be converted
23+
let oi: Result<u32, ParseIntError> = "x".parse();
24+
assert!(oi.is_err());
2325

24-
let i: uint = match from_str("1") {
25-
Some(value) => value,
26-
None => fail!("oops, expected a number")
26+
let i: u32 = match "1".parse() {
27+
Ok(value) => value,
28+
Err(_) => panic!("oops, expected a number")
2729
};
28-
assert_eq!(i, 1u);
30+
assert_eq!(i, 1);
2931

30-
let i: uint = from_str("4").unwrap_or(0u);
31-
assert_eq!(i, 4u);
32+
let i: u32 = "4".parse().unwrap_or(0);
33+
assert_eq!(i, 4);
3234
}

api-std-fs-file.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* http://doc.rust-lang.org/std/fs/struct.File.html
3+
*
4+
* @license MIT license <http://www.opensource.org/licenses/mit-license.php>
5+
*/
6+
use std::io::BufReader;
7+
use std::io::prelude::*;
8+
use std::fs::File;
9+
use std::path::Path;
10+
11+
fn main() {
12+
13+
let mut f = File::open("foo.txt").ok().expect("foo.txt not open");
14+
let mut buffer = Vec::new();
15+
let result = f.read_to_end(&mut buffer);
16+
println!("size: {}", result.ok().expect("foo.txt not read"));
17+
18+
/*
19+
* Read a complete file
20+
*/
21+
let path = Path::new("inifile/src/data/config.ini");
22+
let mut hw_file = File::open(&path).ok().expect("file not open");
23+
let mut data = Vec::new();
24+
match hw_file.read_to_end(&mut data) {
25+
Ok(s) => println!("size: {}, data: {:?}", s, data),
26+
Err(e) => panic!("error while reading {} : {}", path.to_str().unwrap(), e)
27+
}
28+
29+
/*
30+
* Iterate over the lines of a file
31+
*/
32+
let hw_file = File::open(&path).ok().expect("file not open");
33+
let file = BufReader::new(hw_file);
34+
for line in file.lines() {
35+
match line {
36+
Ok(nread) => println!("{}", nread),
37+
Err(e) => println!("error reading: {}", e)
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)