Skip to content

Instantly share code, notes, and snippets.

#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();
@csknk
csknk / match-options-rust.rs
Created May 7, 2020 13:57 — forked from rust-play/playground.rs
Code shared from the Rust Playground
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
@csknk
csknk / alt.rs
Last active May 6, 2020 20:41 — forked from rust-play/playground.rs
Code shared from the Rust Playground
fn main() {
// If `option` is Some<T>, unwrap it and stick the value in `x`.
if let Some(x) = option {
foo(x);
}
}
@csknk
csknk / default-values-from-option.rs
Created May 6, 2020 19:18 — forked from rust-play/playground.rs
Code shared from the Rust Playground
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();
@csknk
csknk / rules-of-borrowing.rs
Last active April 30, 2020 20:45 — forked from rust-play/playground.rs
Code shared from the Rust Playground
/// 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);
@csknk
csknk / error-file-line.c
Created April 1, 2020 13:41
c error message with file & line number
fprintf (stderr, "Internal error: "
"negative string length "
"%d at %s, line %d.",
length, __FILE__, __LINE__);
@csknk
csknk / simple-integer-input-fgets-strtol.c
Last active March 15, 2020 12:02
Simple integer input in C, using `fgets()` to get the entire line as a string and `strtol()` to parse the integer.
#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)) {
@csknk
csknk / unwrap_or_else.rs
Last active March 25, 2023 22:35 — forked from rust-play/playground.rs
unwrap_or_else examples
//! 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")
}
@csknk
csknk / unwrap-or-else-examples.rs
Last active March 6, 2020 18:07 — forked from rust-play/playground.rs
Code shared from the Rust Playground
//! 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")
}
@csknk
csknk / return-multiple-values-from-rust-function.rs
Created March 6, 2020 09:25 — forked from rust-play/playground.rs
Code shared from the Rust Playground
//! 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),