diff options
Diffstat (limited to 'rust.html.markdown')
-rw-r--r-- | rust.html.markdown | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/rust.html.markdown b/rust.html.markdown index 92794e69..526d20d5 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -1,5 +1,5 @@ --- -language: rust +language: Rust contributors: - ["P1start", "http://p1start.github.io/"] filename: learnrust.rs @@ -22,7 +22,7 @@ currently available in the nightly builds. Rust has adopted a train-based releas model with regular releases every six weeks. Rust 1.1 beta was made available at the same time of the release of Rust 1.0. -Although Rust is a relatively low-level language, Rust has some functional +Although Rust is a relatively low-level language, it has some functional concepts that are generally found in higher-level languages. This makes Rust not only fast, but also easy and efficient to code in. @@ -30,6 +30,9 @@ Rust not only fast, but also easy and efficient to code in. // This is a comment. Line comments look like this... // and extend multiple lines like this. +/* Block comments + /* can be nested. */ */ + /// Documentation comments look like this and support markdown notation. /// # Examples /// @@ -89,11 +92,16 @@ fn main() { println!("{} {}", f, x); // 1.3 hello world // A `String` – a heap-allocated string + // Stored as a `Vec<u8>` and always hold a valid UTF-8 sequence, + // which is not null terminated. let s: String = "hello world".to_string(); // A string slice – an immutable view into another string - // The string buffer can be statically allocated like in a string literal - // or contained in another object (in this case, `s`) + // This is basically an immutable pair of pointers to a string – it doesn’t + // actually contain the contents of a string, just a pointer to + // the begin and a pointer to the end of a string buffer, + // statically allocated or contained in another object (in this case, `s`). + // The string slice is like a view `&[u8]` into `Vec<T>`. let s_slice: &str = &s; println!("{} {}", s, s_slice); // hello world hello world @@ -176,13 +184,19 @@ fn main() { impl<T> Foo<T> { // Methods take an explicit `self` parameter - fn get_bar(self) -> T { + fn bar(&self) -> &T { // self is borrowed + &self.bar + } + fn bar_mut(&mut self) -> &mut T { // self is mutably borrowed + &mut self.bar + } + fn into_bar(self) -> T { // here self is consumed self.bar } } let a_foo = Foo { bar: 1 }; - println!("{}", a_foo.get_bar()); // 1 + println!("{}", a_foo.bar()); // 1 // Traits (known as interfaces or typeclasses in other languages) // @@ -199,6 +213,21 @@ fn main() { let another_foo = Foo { bar: 1 }; println!("{:?}", another_foo.frobnicate()); // Some(1) + // Function pointer types // + + fn fibonacci(n: u32) -> u32 { + match n { + 0 => 1, + 1 => 1, + _ => fibonacci(n - 1) + fibonacci(n - 2), + } + } + + type FunctionPointer = fn(u32) -> u32; + + let fib : FunctionPointer = fibonacci; + println!("Fib: {}", fib(4)); // 5 + ///////////////////////// // 3. Pattern matching // ///////////////////////// @@ -230,7 +259,7 @@ fn main() { // `for` loops/iteration let array = [1, 2, 3]; - for i in array.iter() { + for i in array { println!("{}", i); } |