summaryrefslogtreecommitdiffhomepage
path: root/rust.html.markdown
diff options
context:
space:
mode:
authorP1start <rewi-github@whanau.org>2014-07-01 15:24:08 +1200
committerP1start <rewi-github@whanau.org>2014-07-01 15:24:08 +1200
commit9874c063d32e1b9affffac7cca457ffe977c5d71 (patch)
treedc96618110d38cb45567858b75b914986ea4cbd4 /rust.html.markdown
parent83283c0d4ca5f797e505f511b418c3a2f6472d8c (diff)
add &str explanation; add section on vectors
Diffstat (limited to 'rust.html.markdown')
-rw-r--r--rust.html.markdown26
1 files changed, 23 insertions, 3 deletions
diff --git a/rust.html.markdown b/rust.html.markdown
index 532362d1..0b9a5e58 100644
--- a/rust.html.markdown
+++ b/rust.html.markdown
@@ -59,12 +59,32 @@ fn main() {
// Printing
println!("{} {}", f, x); // 1.3 hello world
- // A `String`
+ // A `String` - a heap-allocated string
let s: String = "hello world".to_string();
- // A string slice - a view into another string
+ // A string slice - an immutable view into another string
+ // This is basically an immutable pointer to a string - it doesn’t
+ // actually contain the characters of a string, just a pointer to
+ // something that does (in this case, `s`)
let s_slice: &str = s.as_slice();
+ println!("{} {}", s, s_slice); // hello world hello world
+
+ // Vectors/arrays //
+
+ // A fixed-size array
+ let four_ints: [int, ..4] = [1, 2, 3, 4];
+
+ // A dynamically-sized vector
+ let mut vector: Vec<int> = vec![1, 2, 3, 4];
+ vector.push(5);
+
+ // A slice - an immutable view into a vector or array
+ // This is much like a string slice, but for vectors
+ let slice: &[int] = vector.as_slice();
+
+ println!("{} {}", vector, slice); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
+
//////////////
// 2. Types //
//////////////
@@ -188,7 +208,7 @@ fn main() {
// `if` as expression
let value = if true {
"good"
- else {
+ } else {
"bad"
};