Skip to content

Instantly share code, notes, and snippets.

use std::io;
// See https://learning-rust.github.io/docs/e4.unwrap_and_expect.html
pub fn read_string(msg: &'static str) -> Result<String, String> {
println!("{}", msg);
let mut line = String::new();
match io::stdin().read_line(&mut line) {
Ok(_n) => Ok(line.trim().to_string()),
Err(error) => Err(format!("failed to read line: {}", error)),
@csknk
csknk / return-result-from-main-example-2.rs
Last active March 4, 2020 19:41 — forked from rust-play/playground.rs
Code shared from the Rust Playground
/**
* Contrived function, returns an error if the values are the same.
**/
fn get_largest(a: u32, b: u32) -> Result<u32, &'static str> {
if a == b {
return Err("equal values");
}
if a > b { Ok(a) } else { Ok(b) }
}
@csknk
csknk / return-Result-from-main.rs
Last active March 4, 2020 14:57 — forked from rust-play/playground.rs
Code shared from the Rust Playground
use std::num::ParseIntError;
fn main() -> Result<(), ParseIntError> {
let number_str = "1it";
let number = match number_str.parse::<i32>() {
Ok(number) => number,
Err(e) => return Err(e),
};
println!("{}", number);
Ok(())
@csknk
csknk / change-vector-elements-in-place.rs
Created February 22, 2020 21:06 — forked from rust-play/playground.rs
Code shared from the Rust Playground
/**
* Change vector elements in place.
*/
fn double(v: &mut [i8]) {
for el in v {
*el = *el * 2;
}
}
fn main() {
@csknk
csknk / simple-option-return-from-function.rs
Created February 22, 2020 15:43 — forked from rust-play/playground.rs
Code shared from the Rust Playground
/**
* If the input parameter a is a member of the exclude set,
* return None. otherwise, return a.
*/
fn find_index(v: &[i8], target_value: &i8) -> Option<usize> {
for (i, el) in v.iter().enumerate() {
if el == target_value {
return Some(i);
}
}
@csknk
csknk / simple-demo-function-returns-Option.rs
Created February 22, 2020 15:21 — forked from rust-play/playground.rs
Code shared from the Rust Playground
/**
* If the input parameter a is a member of the exclude set,
* return None. otherwise, return a.
*/
fn conditional_include(a: i8) -> Option<i8> {
let exclude = vec![7, 13, 17];
if exclude.contains(&a) {
return None;
}
Some(a)
@csknk
csknk / mutate_in_function.rs
Last active March 4, 2020 10:39 — forked from rust-play/playground.rs
Code shared from the Rust Playground
#![allow(unused)]
fn compute(input: &u32, output: &mut u32) {
if *input > 5 {
*output = 99;
}
if *input < 5 {
*output = 42;
}
}
@csknk
csknk / GitHub-Forking.md
Created August 12, 2019 19:48 — forked from Chaser324/GitHub-Forking.md
GitHub Standard Fork & Pull Request Workflow

Whether you're trying to give back to the open source community or collaborating on your own projects, knowing how to properly fork and generate pull requests is essential. Unfortunately, it's quite easy to make mistakes or not know what you should do when you're initially learning the process. I know that I certainly had considerable initial trouble with it, and I found a lot of the information on GitHub and around the internet to be rather piecemeal and incomplete - part of the process described here, another there, common hangups in a different place, and so on.

In an attempt to coallate this information for myself and others, this short tutorial is what I've found to be fairly standard procedure for creating a fork, doing your work, issuing a pull request, and merging that pull request back into the original project.

Creating a Fork

Just head over to the GitHub page and click the "Fork" button. It's just that simple. Once you've done that, you can use your favorite git client to clone your repo or j

@csknk
csknk / setup-virtual-environment.sh
Last active August 15, 2018 18:32
Python 3 Virtual Environment
# Set up Python 3 Virtual Environment
apt install python3-venv
# Set up a new venv in `env`
python3 -m venv env
# Activate the venv
. env/bin/activate
# Install dependencies from a requirements.txt file
@csknk
csknk / snippets.sh
Last active August 15, 2018 18:39
Handy BASH one-liners
#!/usr/bin/bash
# Create random passwords in dummy.txt, one per line. `generate-password` defined elsewhere
for i in {0..10}; do echo "$i $(generate-password 32 alnum)" >> dummy.txt; done
# Tar and encrypt a directory
tar -cz original-data-dir | gpg --symmetric -o archive.tgz.gpg
# ...decrypt and untar:
gpg -d archive.tgz.gpg | tar xz