Skip to content

Commit 964fbd7

Browse files
committed
Add api-std-vec.rs, libdate.rs
1 parent e151b09 commit 964fbd7

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Examples are tested with version 0.8.
3131
* Pointer snippets from Dave Herman's talk: `lang-pointers.rs`
3232
* extra::getopts: `api-extra-getopts.rs`
3333
* std::hashmap::HashMap: `api-std-hashmap.rs`
34+
* std::vec: OwnedVector, 2D-arrays, ...: `api-std-vec.rs`
3435
* Some new files:
3536
* `Makefile` to compile, run tests and run benchmarks
3637
* A library and its unit tests and benchmarks for 2 Fibonacci functions (a reccursive and a non reccursive): `libfibonacci.rs`

api-std-vec.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* http://static.rust-lang.org/doc/0.8/std/vec/index.html
3+
*
4+
* @license MIT license <http://www.opensource.org/licenses/mit-license.php>
5+
*/
6+
use std::vec;
7+
8+
/**
9+
* ~[~str] is an owned pointer, allocated on the send heap, can be sent accross tasks.
10+
*/
11+
fn fillStrings() -> ~[~str] {
12+
let mut strings: ~[~str] = ~[];
13+
strings.push(~"hello");
14+
strings.push(~"world");
15+
strings
16+
}
17+
18+
/**
19+
* Use of vec::from_elem to create a dynamic two dimensional array.
20+
*/
21+
fn make2dArray(dim1: uint, dim2: uint, default: int) -> ~[~[int]] {
22+
vec::from_elem(dim1, vec::from_elem(dim2, default))
23+
}
24+
25+
fn main() {
26+
/*
27+
* Simple use
28+
* .iter() returns an iteration value for a vector or a vector slice.
29+
* The iterator yields borrowed pointers to the vector's elements,
30+
* so if the element type of the vector is int, the element type of the iterator is &int.
31+
*/
32+
let numbers = [0, 1, 2];
33+
for &x in numbers.iter() {
34+
println!("element in numbers: {}", x);
35+
}
36+
37+
/*
38+
* .slice(a, b) returns an immutable "view" into a vector or a vector slice from the interval (a, b)
39+
* .push() adds an item at the end of an OwnedVector
40+
*/
41+
let mut numbers2 = ~[];
42+
for &x in numbers.slice(1, 3).iter() {
43+
numbers2.push(x);
44+
}
45+
for &x in numbers2.iter() {
46+
println!("element in numbers2: {}", x);
47+
}
48+
49+
/*
50+
* Create a vector in a function.
51+
*/
52+
let mut strings = fillStrings();
53+
println!("strings[1] = {}", strings[1]);
54+
strings[1] = ~"me";
55+
println!("strings[1] = {}", strings[1]);
56+
57+
/*
58+
* Create a two dimensional vector.
59+
*/
60+
let mut array2d = [[0u8, ..4], ..4];
61+
println!("array2d[0][0] = {}", array2d[0][0]);
62+
array2d[0][1] = 1;
63+
println!("array2d[0][1] = {}", array2d[0][1]);
64+
65+
/*
66+
* Create a two dimensional dynamic vector.
67+
*/
68+
let mut anotherArray2d = make2dArray(2, 3, -1);
69+
println!("anotherArray2d[0][0] = {}", anotherArray2d[0][0]);
70+
anotherArray2d[0][1] = 1;
71+
println!("anotherArray2d[0][1] = {}", anotherArray2d[0][1]);
72+
}
73+

libdate.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#[link(name = "date", vers = "1.0", author = "eliovir")];
2+
#[crate_type = "lib"];
3+
#[license = "MIT"];
4+
#[desc = "Library for simple date management" ];
5+
#[comment = "Example of library: date management"];
16
/**
27
* Date management
38
*

tutorial-14-closure.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@
44
*
55
* @license MIT license <http://www.opensource.org/licenses/mit-license.php>
66
*/
7-
fn call_closure_with_ten(b: &fn(int)) { b(10); }
7+
8+
fn apply(i: int, f: &fn(int)->int) -> int {
9+
f(i)
10+
}
11+
12+
fn call_closure_with_ten(f: &fn(int)) {
13+
f(10);
14+
}
815

916
fn main() {
17+
/*
18+
* Simple call of closure
19+
*/
20+
let res = apply(4, |x| { x*x});
21+
println(fmt!("apply(4, |x| { x*x}) => %d", res));
22+
23+
/*
24+
* Call with captured variable
25+
*/
1026
let captured_var = 20;
1127
let closure = |arg| println(fmt!("captured_var=%d, arg=%d", captured_var, arg));
1228
call_closure_with_ten(closure);
29+
1330
/*
31+
* Use of .map() on a vector
1432
*/
1533
let mut max = 0i;
1634
[1, 2, 3].map(|x| if *x > max { max = *x });

0 commit comments

Comments
 (0)