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 b = box 15i; // `b` is the single owner of this `Box<int>` | |
let c = &b; // I can have a reference to `b` | |
println!("{}", c); // Unlike C++, Rust doesn't require dereferencing here | |
} |
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
// You can only have a single owner to a particular memory space. | |
fn main() { | |
let x = box 5i; // create an owned pointer "x" to the value 5i using the "box" keyword | |
let y = x; // "y" now owns 5i | |
println!("{}", x); // ERROR: "x" is no longer the owner 5i's memory space | |
} |
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 x = box 5i; // Creates a box | |
let y = x; // Transfers ownership to "y" | |
let x = &y; // "x" now references what "y" owns | |
println!("{}", x); // This will compile successfully | |
} |
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() { | |
// Example of a &str to a String conversion | |
use std::str; | |
let str_slice = "Here is a \"&'static str\""; | |
let str_repl = str::replace(str_slice, "is", "was"); // String replacement | |
println!("{}, now it's a \"String\".", str_repl.to_string()); // String conversion | |
// To go from a String to a &str, use the as_slice() method. | |
} |
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
static Pi: f64 = 3.14; | |
fn display_pi() { | |
println!("{}", Pi); | |
} | |
fn main() { | |
display_pi() | |
} |
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
use std::io; | |
fn main() { | |
println!("Type something:"); | |
let input = io::stdin() | |
.read_line() // read user input | |
.ok().expect("unable to read input!"); // Display this in case of a problem reading user input | |
// convert to &str and get rid of the newline |
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
use std::io; | |
fn add(x: int, y: int) -> int { // takes two integers and returns an integer | |
x + y | |
} | |
fn main() { | |
println!("Simple Addition Calculator, written in Rust!"); | |
println!("Enter 1st number: "); |
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 date = ("Today", 4i, 8i, 2014i); // simple tuple | |
println!("{}", date); | |
let (wd, d, m, y) = ("Today", 4i, 8i, 2014i); // destructuring let | |
println!("{} is {}/{}/{}", wd, d, m, y); // accessing individual variables from the tuple | |
} |
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() { | |
struct Date { | |
m: int, | |
y: int | |
} | |
let today = Date {m: 8i, y: 2014i}; | |
println!("\"August\" is the {}th month in {} (and every other year)", today.m, today.y); // access individual fields | |
} |
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
/* http://prismjs.com/download.html?themes=prism&languages=markup+css+clike+javascript+apacheconf+bash+c+cpp+coffeescript+css-extras+dart+erlang+fsharp+git+go+haml+handlebars+haskell+http+jade+java+julia+latex+less+markdown+nasm+perl+php+php-extras+python+r+jsx+rest+ruby+rust+sas+scss+scala+sql+swift+typescript+wiki+yaml&plugins=autolinker+file-highlight+show-language+highlight-keywords */ | |
/** | |
* prism.js default theme for JavaScript, CSS and HTML | |
* Based on dabblet (http://dabblet.com) | |
* @author Lea Verou | |
*/ | |
code[class*="language-"], | |
pre[class*="language-"] { | |
color: black; |