From e620daf3b1a40ae825c625bc4328fb7d2b3ee8b6 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 2 Jul 2014 02:54:21 -0400 Subject: initial commit --- examples/00_intro.txt | 24 ++++++++++++++++ examples/01_hello.rs | 4 +++ examples/02_sample.rs | 22 +++++++++++++++ examples/03_sample-filter.rs | 22 +++++++++++++++ examples/04_point.rs | 10 +++++++ examples/05_enum.rs | 11 ++++++++ examples/06_enum-nonexhaustive.rs | 11 ++++++++ examples/07_enum-unknown-value.rs | 11 ++++++++ examples/08_enum-custom.rs | 21 ++++++++++++++ examples/09_option.rs | 5 ++++ examples/10_vec.rs | 10 +++++++ examples/11_str.rs | 8 ++++++ examples/12_point-impl.rs | 16 +++++++++++ examples/13_trait.rs | 24 ++++++++++++++++ examples/14_point-show.rs | 19 +++++++++++++ examples/15_borrow-error.cpp | 14 ++++++++++ examples/16_borrow-error.rs | 10 +++++++ examples/17_borrow-error-fixed.rs | 10 +++++++ examples/18_ownership_error.rs | 6 ++++ examples/19_list-error.rs | 12 ++++++++ examples/20_list.rs | 12 ++++++++ examples/21_spawn.rs | 24 ++++++++++++++++ examples/22_see_also.txt | 18 ++++++++++++ examples/Makefile | 58 +++++++++++++++++++++++++++++++++++++++ 24 files changed, 382 insertions(+) create mode 100644 examples/00_intro.txt create mode 100644 examples/01_hello.rs create mode 100644 examples/02_sample.rs create mode 100644 examples/03_sample-filter.rs create mode 100644 examples/04_point.rs create mode 100644 examples/05_enum.rs create mode 100644 examples/06_enum-nonexhaustive.rs create mode 100644 examples/07_enum-unknown-value.rs create mode 100644 examples/08_enum-custom.rs create mode 100644 examples/09_option.rs create mode 100644 examples/10_vec.rs create mode 100644 examples/11_str.rs create mode 100644 examples/12_point-impl.rs create mode 100644 examples/13_trait.rs create mode 100644 examples/14_point-show.rs create mode 100644 examples/15_borrow-error.cpp create mode 100644 examples/16_borrow-error.rs create mode 100644 examples/17_borrow-error-fixed.rs create mode 100644 examples/18_ownership_error.rs create mode 100644 examples/19_list-error.rs create mode 100644 examples/20_list.rs create mode 100644 examples/21_spawn.rs create mode 100644 examples/22_see_also.txt create mode 100644 examples/Makefile (limited to 'examples') diff --git a/examples/00_intro.txt b/examples/00_intro.txt new file mode 100644 index 0000000..4c24d12 --- /dev/null +++ b/examples/00_intro.txt @@ -0,0 +1,24 @@ + + + + + + + + + + + + Introduction to Rust + Jesse Luehrs + + + + + + + + + + + diff --git a/examples/01_hello.rs b/examples/01_hello.rs new file mode 100644 index 0000000..056d90f --- /dev/null +++ b/examples/01_hello.rs @@ -0,0 +1,4 @@ +fn main () { + println!("Hello world!") +} + diff --git a/examples/02_sample.rs b/examples/02_sample.rs new file mode 100644 index 0000000..4df0563 --- /dev/null +++ b/examples/02_sample.rs @@ -0,0 +1,22 @@ +fn main() { + // A simple integer calculator: + // `+` or `-` means add or subtract by 1 + // `*` or `/` means multiply or divide by 2 + + let program = "+ + * - /"; + let mut accumulator = 0i; + + for token in program.chars() { + match token { + '+' => accumulator += 1, + '-' => accumulator -= 1, + '*' => accumulator *= 2, + '/' => accumulator /= 2, + _ => { /* ignore everything else */ } + } + } + + println!("The program \"{}\" calculates the value {}", + program, accumulator); +} + diff --git a/examples/03_sample-filter.rs b/examples/03_sample-filter.rs new file mode 100644 index 0000000..3fc0bff --- /dev/null +++ b/examples/03_sample-filter.rs @@ -0,0 +1,22 @@ +fn main() { + // A simple integer calculator: + // `+` or `-` means add or subtract by 1 + // `*` or `/` means multiply or divide by 2 + + let program = "+ + * - /"; + let mut accumulator = 0i; + + for token in program.chars().filter(|&x| { x != ' ' }) { + match token { + '+' => accumulator += 1, + '-' => accumulator -= 1, + '*' => accumulator *= 2, + '/' => accumulator /= 2, + _ => fail!("unknown character"), + } + } + + println!("The program \"{}\" calculates the value {}", + program, accumulator); +} + diff --git a/examples/04_point.rs b/examples/04_point.rs new file mode 100644 index 0000000..a5b1910 --- /dev/null +++ b/examples/04_point.rs @@ -0,0 +1,10 @@ +struct Point { + x: int, + y: int, +} + +fn main () { + let p1 = Point { x: 1, y: 2 }; + println!("({}, {})", p1.x, p1.y); +} + diff --git a/examples/05_enum.rs b/examples/05_enum.rs new file mode 100644 index 0000000..304ad04 --- /dev/null +++ b/examples/05_enum.rs @@ -0,0 +1,11 @@ +enum Color { + Red, + Green, + Blue, +} + +fn main () { + let c = Red; + println!("{}", match c { Red => "r", Green => "g", Blue => "b" }); +} + diff --git a/examples/06_enum-nonexhaustive.rs b/examples/06_enum-nonexhaustive.rs new file mode 100644 index 0000000..14d5bc8 --- /dev/null +++ b/examples/06_enum-nonexhaustive.rs @@ -0,0 +1,11 @@ +enum Color { + Red, + Green, + Blue, +} + +fn main () { + let c = Red; + println!("{}", match c { Red => "r", Green => "g" }); +} + diff --git a/examples/07_enum-unknown-value.rs b/examples/07_enum-unknown-value.rs new file mode 100644 index 0000000..76cc2d3 --- /dev/null +++ b/examples/07_enum-unknown-value.rs @@ -0,0 +1,11 @@ +enum Color { + Red, + Green, + Blue, +} + +fn main () { + let c = Red; + println!("{}", match c { Red => "r", Green => "g", Blue => "b", 27 => "a" }); +} + diff --git a/examples/08_enum-custom.rs b/examples/08_enum-custom.rs new file mode 100644 index 0000000..48d3691 --- /dev/null +++ b/examples/08_enum-custom.rs @@ -0,0 +1,21 @@ +enum Color { + Red, + Green, + Blue, + Custom(int, int, int), +} + +fn print_color (c: Color) { + match c { + Red => println!("#ff0000"), + Green => println!("#00ff00"), + Blue => println!("#0000ff"), + Custom(r, g, b) => println!("#{:02x}{:02x}{:02x}", r, g, b), + } +} + +fn main () { + print_color(Red); + print_color(Custom(0x12, 0x45, 0xba)); +} + diff --git a/examples/09_option.rs b/examples/09_option.rs new file mode 100644 index 0000000..ca06399 --- /dev/null +++ b/examples/09_option.rs @@ -0,0 +1,5 @@ +enum Option { + Some(T), + None, +} + diff --git a/examples/10_vec.rs b/examples/10_vec.rs new file mode 100644 index 0000000..eb00960 --- /dev/null +++ b/examples/10_vec.rs @@ -0,0 +1,10 @@ +fn main () { + let mut v = vec![6i, 8i, 22i]; + v.push(58); + + println!("{}:", v.len()); + for value in v.iter() { + println!(" {}", value); + } +} + diff --git a/examples/11_str.rs b/examples/11_str.rs new file mode 100644 index 0000000..7b40fc4 --- /dev/null +++ b/examples/11_str.rs @@ -0,0 +1,8 @@ +fn main () { + let mut s = String::from_str("Hello"); + s.push_str(" world"); + + println!("{}", s.len()); + println!("{}", s); +} + diff --git a/examples/12_point-impl.rs b/examples/12_point-impl.rs new file mode 100644 index 0000000..302f787 --- /dev/null +++ b/examples/12_point-impl.rs @@ -0,0 +1,16 @@ +struct Point { + x: int, + y: int, +} + +impl Point { + fn new (x: int, y: int) -> Point { Point { x: x, y: y } } + fn x (&self) -> int { self.x } + fn y (&self) -> int { self.y } +} + +fn main () { + let p1 = Point::new(1, 2); + println!("({}, {})", p1.x(), p1.y()); +} + diff --git a/examples/13_trait.rs b/examples/13_trait.rs new file mode 100644 index 0000000..8976d0a --- /dev/null +++ b/examples/13_trait.rs @@ -0,0 +1,24 @@ +trait Area { + fn area (&self) -> f64; +} +struct Rectangle { + width: f64, + height: f64, +} +impl Area for Rectangle { + fn area (&self) -> f64 { self.width * self.height } +} +struct Circle { + radius: f64, +} +impl Area for Circle { + fn area (&self) -> f64 { 3.14 * self.radius * self.radius } +} +fn print_area (shape: T) { + println!("{}", shape.area()); +} +fn main () { + print_area(Circle { radius: 2.0 }); + print_area(Rectangle { width: 3.2, height: 4.5 }); +} + diff --git a/examples/14_point-show.rs b/examples/14_point-show.rs new file mode 100644 index 0000000..fb21ab1 --- /dev/null +++ b/examples/14_point-show.rs @@ -0,0 +1,19 @@ +use std::fmt::{Show,Formatter,FormatError}; + +struct Point { + x: int, + y: int, +} + +impl Show for Point { + fn fmt (&self, f: &mut Formatter) -> Result<(), FormatError> { + try!(write!(f, "({}, {})", self.x, self.y)); + return Ok(()); + } +} + +fn main () { + let p1 = Point { x: 1, y: 2 }; + println!("{}", p1); +} + diff --git a/examples/15_borrow-error.cpp b/examples/15_borrow-error.cpp new file mode 100644 index 0000000..74213ce --- /dev/null +++ b/examples/15_borrow-error.cpp @@ -0,0 +1,14 @@ +#include + +int *foo(void) +{ + int x = 2; + return &x; +} + +int main(int argc, char *argv[]) +{ + int *x = foo(); + std::cout << *x << std::endl; +} + diff --git a/examples/16_borrow-error.rs b/examples/16_borrow-error.rs new file mode 100644 index 0000000..eefe233 --- /dev/null +++ b/examples/16_borrow-error.rs @@ -0,0 +1,10 @@ +fn foo () -> &int { + let x = 2; + return &x; +} + +fn main () { + let x = foo(); + println!("{}", *x); +} + diff --git a/examples/17_borrow-error-fixed.rs b/examples/17_borrow-error-fixed.rs new file mode 100644 index 0000000..06ff060 --- /dev/null +++ b/examples/17_borrow-error-fixed.rs @@ -0,0 +1,10 @@ +fn foo () -> Box { + let x = box 2; + return x; +} + +fn main () { + let x = foo(); + println!("{}", *x); +} + diff --git a/examples/18_ownership_error.rs b/examples/18_ownership_error.rs new file mode 100644 index 0000000..0e791c6 --- /dev/null +++ b/examples/18_ownership_error.rs @@ -0,0 +1,6 @@ +fn main () { + let x = box 2; + let y = x; + let z = x; +} + diff --git a/examples/19_list-error.rs b/examples/19_list-error.rs new file mode 100644 index 0000000..836e346 --- /dev/null +++ b/examples/19_list-error.rs @@ -0,0 +1,12 @@ +extern crate debug; + +enum List { + Cons(T, List), + Nil +} + +fn main () { + let l = Cons(1, Cons(2, Cons(4, Nil))); + println!("{:?}", l); +} + diff --git a/examples/20_list.rs b/examples/20_list.rs new file mode 100644 index 0000000..a8b7e6f --- /dev/null +++ b/examples/20_list.rs @@ -0,0 +1,12 @@ +extern crate debug; + +enum List { + Cons(T, Box>), + Nil +} + +fn main () { + let l = box Cons(1i, box Cons(2i, box Cons(4i, box Nil))); + println!("{:?}", l); +} + diff --git a/examples/21_spawn.rs b/examples/21_spawn.rs new file mode 100644 index 0000000..770bb21 --- /dev/null +++ b/examples/21_spawn.rs @@ -0,0 +1,24 @@ +fn ack (m: uint, n: uint) -> uint { + match (m, n) { + (0, n) => n + 1, + (m, 0) => ack(m - 1, 1), + (m, n) => ack(m - 1, ack(m, n - 1)), + } +} +fn main () { + let (write, read) = channel(); + let (m, n) = (3, 10); + + spawn(proc () { + write.send(ack(m, n)); + }); + + let mut result = read.try_recv(); + while !result.is_ok() { + println!("."); + std::io::timer::sleep(100); + result = read.try_recv(); + } + println!("Ack({}, {}) = {}", m, n, result.unwrap_or_else(|e| { fail!(e) })); +} + diff --git a/examples/22_see_also.txt b/examples/22_see_also.txt new file mode 100644 index 0000000..6f8c780 --- /dev/null +++ b/examples/22_see_also.txt @@ -0,0 +1,18 @@ +Official Sites +============== +Website: http://rust-lang.org/ +IRC channel: irc://irc.mozilla.org/#rust +Mailing List: https://mail.mozilla.org/pipermail/rust-dev/ +Repository: https://github.com/rust-lang/rust + +Learning Rust +============= +http://doc.rust-lang.org/ +http://play.rust-lang.org/ +http://rustbyexample.com/ + +Contributing to Rust +==================== +http://blog.octayn.net/ +https://github.com/rust-lang/rust/wiki/Note-development-policy + diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..0f42308 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,58 @@ +OUT = 01_hello \ + 02_sample \ + 03_sample-filter \ + 04_point \ + 05_enum \ + 08_enum-custom \ + 10_vec \ + 11_str \ + 12_point-impl \ + 13_trait \ + 14_point-show \ + 17_borrow-error-fixed \ + 20_list \ + 21_spawn + +all : $(OUT) + +1 : 01_hello + ./01_hello +2 : 02_sample + ./02_sample +3 : 03_sample-filter + ./03_sample-filter +4 : 04_point + ./04_point +5 : 05_enum + ./05_enum +6 : 06_enum-nonexhaustive +7 : 07_enum-unknown-value +8 : 08_enum-custom + ./08_enum-custom +10 : 10_vec + ./10_vec +11 : 11_str + ./11_str +12 : 12_point-impl + ./12_point-impl +13 : 13_trait + ./13_trait +14 : 14_point-show + ./14_point-show +16 : 16_borrow-error +17 : 17_borrow-error-fixed + ./17_borrow-error-fixed +18 : 18_ownership_error +19 : 19_list-error +20 : 20_list + ./20_list +21 : 21_spawn + ./21_spawn + +% : %.rs + rustc $< + +clean: + rm -f $(OUT) + +.PHONY: clean -- cgit v1.2.3