Skip to content

Instantly share code, notes, and snippets.

@MatejLach
MatejLach / constant.rs
Created August 1, 2014 15:46
Example of a global constant in Rust
static Pi: f64 = 3.14;
fn display_pi() {
println!("{}", Pi);
}
fn main() {
display_pi()
}
@MatejLach
MatejLach / &str2String.rs
Created August 1, 2014 14:53
An example of how to convert a &'static str to a String in Rust
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.
}
@MatejLach
MatejLach / reference.rs
Created July 30, 2014 15:50
An example of borrowing using references in Rust...
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
}
@MatejLach
MatejLach / ownedpointer_error.rs
Created July 30, 2014 15:22
Showcases how ownership is moved about in Rust...
// 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
}
@MatejLach
MatejLach / owned_pointer.rs
Created July 30, 2014 14:41
An owned Rust pointer example...
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
}
@MatejLach
MatejLach / for_while.rs
Created July 29, 2014 15:35
An example of a nested for/while loop in Rust...
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;
}
@MatejLach
MatejLach / ifelse.rs
Created July 29, 2014 14:06
if/else Rust example...
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 {
@MatejLach
MatejLach / patternmatch.rs
Created July 23, 2014 12:35
Pattern matching in Rust...
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`
@MatejLach
MatejLach / euler1.rs
Created July 21, 2014 13:58
Demonstrates a case where one has to use mutable variables in Rust
// 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);
@MatejLach
MatejLach / hello_world.rs
Created July 20, 2014 10:59
Hello World in Rust...
fn main() {
println!("Hello Rust!");
}