This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub fn say_hi() { | |
| println("Hi!"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern mod std; | |
| use std::time; | |
| struct User { | |
| name: ~str, | |
| age: int | |
| } | |
| impl User { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Point { x: int, y:int } | |
| fn main() { | |
| // Local variable, easy | |
| let mut p1 = Point{x:1, y:2}; | |
| p1.x = 10; | |
| // Owned pointer | |
| // also easy because target inherts mutability |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let name: ~str, | |
| other: ~str; | |
| name = ~"Bob"; | |
| other = name; | |
| println(other); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern mod sqlite; | |
| fn db() { | |
| let database = | |
| match sqlite::open("test.db") { | |
| Ok(db) => db, | |
| Err(e) => { | |
| println(fmt!("Error opening test.db: %?", e)); | |
| return; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern mod std; | |
| use std::{net_tcp,net_ip}; | |
| use std::uv; | |
| fn fetch(code: ~str) -> ~[~str] { | |
| let ipaddr = net_ip::v4::parse_addr("205.156.51.232"); | |
| let iotask = uv::global_loop::get(); | |
| let connect_result = net_tcp::connect(ipaddr, 80, &iotask); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn load(filename: ~str) -> ~[~str] { | |
| // The simple way: | |
| // let read_result = io::file_reader(~path::Path(filename)); | |
| let read_result: Result<@Reader, ~str>; | |
| read_result = io::file_reader(~path::Path(filename)); | |
| match read_result { | |
| Ok(file) => return file.read_lines(), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn load(filename: ~str) -> ~[~str] { | |
| // The simple way: | |
| // let read_result = io::file_reader(~path::Path(filename)); | |
| let read_result: Result<@Reader, ~str>; | |
| read_result = io::file_reader(~path::Path(filename)); | |
| if read_result.is_ok() { | |
| let file = read_result.unwrap(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| for 2.times { | |
| println("Basic loop sugar") | |
| } | |
| 2.times(||{ println("Basic loop closure"); true }); | |
| for [1,2,3].each |var| { | |
| println(fmt!("Sugary loop %d", *var)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Ask the user for their name */ | |
| fn ask_name(prompt: ~str) -> ~str { | |
| println(prompt); | |
| return io::stdin().read_line(); | |
| } | |
| fn main() { | |
| let name = ask_name(~"What is your name?"); | |
| println(fmt!("Hello %s", name)); | |
| } |