initial, up to chapter 9
This commit is contained in:
commit
6270aaee83
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
84
Cargo.lock
generated
Normal file
84
Cargo.lock
generated
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "0.1.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.1.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.79"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.7.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
"libc",
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
"rand_hc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_hc"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
|
||||||
|
dependencies = [
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rusty-playground"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.9.0+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
|
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "rusty-playground"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["aj <andrewjpack@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.7.3"
|
4
README.md
Normal file
4
README.md
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Rusty Playground
|
||||||
|
==================
|
||||||
|
|
||||||
|
Playing around with rust, writing a reference doc
|
4
src/collections.rs
Normal file
4
src/collections.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
fn hello_world(){
|
||||||
|
|
||||||
|
}
|
181
src/main.rs
Normal file
181
src/main.rs
Normal file
@ -0,0 +1,181 @@
|
|||||||
|
use std::io;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
mod collections;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
basics();
|
||||||
|
compare();
|
||||||
|
loops();
|
||||||
|
vars();
|
||||||
|
compound();
|
||||||
|
|
||||||
|
println!("testing returns: {}", returning(2));
|
||||||
|
strings();
|
||||||
|
nullables();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn basics() {
|
||||||
|
// ! indicates a macro not a func
|
||||||
|
println!("Enter a number: ");
|
||||||
|
|
||||||
|
// mut defines a mutable variable
|
||||||
|
let mut input = String::new();
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut input)
|
||||||
|
// nice error handling wrapper on Result from read_line
|
||||||
|
.expect("Failed to read line");
|
||||||
|
|
||||||
|
// shadow previously defined variable
|
||||||
|
// i32, u32, i64 (u = unsigned)
|
||||||
|
let input: u32 = match input.trim() // std str trim (\n)
|
||||||
|
.parse() // infers what to parse to using type annotation
|
||||||
|
{
|
||||||
|
Ok(num) => num,
|
||||||
|
Err(_) => 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("You entered: {}", input);
|
||||||
|
|
||||||
|
// thread local random number gener
|
||||||
|
let r_num = rand::thread_rng().gen_range(1, 101);
|
||||||
|
println!("random number: {}", r_num);
|
||||||
|
|
||||||
|
let vec = build_vector3(1, 2, 3);
|
||||||
|
println!("Vector: {}/{}/{}", vec.x, vec.y, vec.z);
|
||||||
|
let vec2 = update_x(vec);
|
||||||
|
println!("Vector2: {:#?}", vec2)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compare() {
|
||||||
|
let first = 5;
|
||||||
|
let second = 6;
|
||||||
|
|
||||||
|
match first.cmp(&second) {
|
||||||
|
Ordering::Less => println!("Smaller"),
|
||||||
|
Ordering::Greater => println!("Bigger"),
|
||||||
|
Ordering::Equal => println!("Equal"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn loops() {
|
||||||
|
let var = 2;
|
||||||
|
loop {
|
||||||
|
println!("Looping!");
|
||||||
|
|
||||||
|
if var == 2 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Done with loop");
|
||||||
|
|
||||||
|
let mut count = 0;
|
||||||
|
let mut result = loop {
|
||||||
|
count += 1;
|
||||||
|
|
||||||
|
if count == 4 {
|
||||||
|
break count *6;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
while result != 0 {
|
||||||
|
result -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let iterables = [1, 2, 3, 4];
|
||||||
|
for el in iterables.iter() {
|
||||||
|
println!("iterable value: {}", el);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6 exclusive
|
||||||
|
for num in (1..6).rev() {
|
||||||
|
println!("ranged values: {}", num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn vars() {
|
||||||
|
// must be type annotated
|
||||||
|
const X: i32 = 3_000_000;
|
||||||
|
println!("const: {}", X);
|
||||||
|
|
||||||
|
// suffix type on a scalar literal
|
||||||
|
let _literal = 28u8;
|
||||||
|
let _f = 2.0; // defaults to 64 bit
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compound() {
|
||||||
|
let tup = (500, 'a');
|
||||||
|
let (_, second) = tup;
|
||||||
|
println!("tuple: {}/{}", tup.0, second);
|
||||||
|
|
||||||
|
let _arr: [u8; 4] = [1, 2, 3, 4];
|
||||||
|
let repeater = [3; 5]; // stretches to 5 elements of 3
|
||||||
|
println!("Retrieving from array: {}", repeater[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn returning(input: i32) -> i32 {
|
||||||
|
let _stuff = 0;
|
||||||
|
|
||||||
|
let condition = true;
|
||||||
|
let number = if condition { 5 } else { 6 };
|
||||||
|
println!("The value of number is: {}", number);
|
||||||
|
|
||||||
|
input + 1 // no semicolon, return value
|
||||||
|
}
|
||||||
|
|
||||||
|
fn strings() {
|
||||||
|
let mut s = String::from("hello");
|
||||||
|
s.push_str(", world!");
|
||||||
|
println!("Sliced: {}", &s[4..8]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Vector3 {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
z: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Vector3 {
|
||||||
|
fn dot(&self, other: &Vector3) -> i32 {
|
||||||
|
self.x * other.x
|
||||||
|
+
|
||||||
|
self.y * other.y
|
||||||
|
+
|
||||||
|
self.z * other.z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_vector3(x: i32, y: i32, z: i32) -> Vector3 {
|
||||||
|
Vector3 {
|
||||||
|
x, y, z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_x(vec: Vector3) -> Vector3 {
|
||||||
|
Vector3 {
|
||||||
|
x: 5,
|
||||||
|
..vec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ColourChannel {
|
||||||
|
Red,
|
||||||
|
Green,
|
||||||
|
Blue
|
||||||
|
}
|
||||||
|
|
||||||
|
enum IpAddr {
|
||||||
|
V4(u8, u8, u8, u8),
|
||||||
|
V6(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn nullables() {
|
||||||
|
let _some_number = Some(5);
|
||||||
|
let _some_string = Some("a string");
|
||||||
|
|
||||||
|
let _absent_number: Option<i32> = None;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user