This file contains 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
#include <iostream> | |
#include <limits> | |
int main() { | |
int input = 0; | |
while (1) { | |
std::cout << "Enter a number (-1 to quit): "; | |
if (!(std::cin >> input)) { | |
std::cout << "You entered a non-numeric value..." << std::endl; | |
std::cin.clear(); |
This file contains 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() { | |
// Match is an expression as well as a statement. This example shows how | |
// a variable is set based on an Option enum. | |
// ------------------------------------------ | |
let msg_option = Some("+++MELON MELON MELON+++"); // Some variant | |
let msg_option_empty = None; // None variant | |
let msg = match msg_option { | |
Some(m) => m, // m is the unwrapped data from the option |
This file contains 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() { | |
// If `option` is Some<T>, unwrap it and stick the value in `x`. | |
if let Some(x) = option { | |
foo(x); | |
} | |
} |
This file contains 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() { | |
// If x has a value, unwrap it and set y to it's value. | |
// Otherwise, set y to a default. | |
// ------------------------------------------------------------------------ | |
// let x = Some(11); // Uncomment to test | |
let x = None; | |
let y; | |
if x.is_some() { | |
y = x.unwrap(); |
This file contains 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
/// Notes ownership and borrowing. | |
fn main() -> Result<(), &'static str> { | |
let mut a = [1,2,3,4]; | |
println!("{:?}", a); | |
{ | |
let b = &mut a[0..2]; | |
// You can't access a at this point because it has been mutably borrowed | |
// from. The following line won't compile, with the error message: | |
// `cannot borrow `a` as immutable because it is also borrowed as mutable`: | |
// println!("a: {:?}", a); |
This file contains 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
fprintf (stderr, "Internal error: " | |
"negative string length " | |
"%d at %s, line %d.", | |
length, __FILE__, __LINE__); |
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
char* end = NULL; | |
char buf[255]; | |
long n = 0; | |
printf("Enter an integer:\n"); | |
while (fgets(buf, sizeof(buf), stdin)) { |
This file contains 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
//! Examples for `unwrap_or_else()` | |
use std::process; | |
fn func(in_num: u8) -> Option<&'static str> { | |
if in_num % 2 != 0 { | |
return None; | |
} | |
Some("even") | |
} |
This file contains 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
//! Examples for `unwrap_or_else()` | |
use std::process; | |
fn func(in_num: u8) -> Option<&'static str> { | |
if in_num % 2 != 0 { | |
return None; | |
} | |
Some("even") | |
} |
This file contains 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
//! Return multiple values from a function - | |
//! Rust makes this easy. | |
fn many() -> Result<(&'static str, &'static str), &'static str> { | |
Ok(("rocky", "ronnie")) | |
} | |
fn main() -> Result<(), &'static str> { | |
let (a, b) = match many() { | |
Ok((a, b)) => (a, b), |