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
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
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
// 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 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
fn main() { | |
let lang = "Rust"; | |
let rust_is_amazing = true; | |
let mut count = 0i; | |
for _ in range(0i, 5) { | |
while count < 10 && rust_is_amazing == true { | |
println!("Is, {} amazing? {}", lang, rust_is_amazing); | |
count += 1; | |
} |
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 lang = "Rust"; | |
if lang == "Rust" { | |
println!("The next-gen systems programming language!"); | |
} else if lang == "Haskell" { | |
println!("A high-level, purely functional programming language."); | |
} else if lang == "Elixir" { | |
println!("Another aspiring, functional language."); | |
} else { |
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 = 3.75f64; | |
match x { | |
1.0..1.99 => println!("x is between one and two"), | |
2.0..2.99 => println!("x is between two and three"), | |
3.0..3.99 => println!("x is between three and four"), | |
4.0..4.99 => println!("x is between four and five"), | |
5.0..5.99 => println!("x is between five and six"), | |
_ => println!("x is bigger than five") // catches all other possible values of `x` |
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
// Solution to Project Euler problem 1, | |
// https://projecteuler.net/problem=1 | |
fn main() { | |
let (i, result) = (0i, 0i); | |
while i < 1000 { | |
if i%3 == 0 || i%5 == 0 { result += i } | |
i += 1 | |
} | |
println!("{}", result); |
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() { | |
println!("Hello Rust!"); | |
} |