From 9874c063d32e1b9affffac7cca457ffe977c5d71 Mon Sep 17 00:00:00 2001 From: P1start Date: Tue, 1 Jul 2014 15:24:08 +1200 Subject: add &str explanation; add section on vectors --- rust.html.markdown | 26 +++++++++++++++++++++++--- 1 file 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 = 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" }; -- cgit v1.2.3