Giter VIP home page Giter VIP logo

aoc2019's Introduction

Advent of code 2019 in Rust

Attempt to solve aoc2019 in rust.

Learned stuff

Iterator.step_by

Iterator.windows

successors

frickin' comparing floats to one another

radian (I am stupid)

f64.to_radians

atan2 to find an angle for a given point (day10)

while let

match

match guard

permutations of a sequence (language agnostic)

measure time (for benchmarking functions)

split_at_mut, when borrowing mutliple elements in a vec things get weird (see day 12 "step" fn)

I am no maths guy...

sharing variables across threads; arc and mutex


Ownership

let v = vec![1, 2, 3];

let mut v2 = v;
// accessing v is invalid, because v moved ownership to v2.
// this keeps us from i.e. changing v2 length to 2 and then accessing the v.3 element

// keywords: ownership, binding(pointer)

// # copy types
// uses the Copy trait, which copies the data on assign

let v = 1
let v2 = v
println!("v is: {}"); // this works, because i32 is copied

References and borrowing

https://doc.rust-lang.org/1.9.0/book/references-and-borrowing.html

// #'&' means to borrow a resource, but not being able to change it
fn foo(v1: &Vec<i32>, v2: &Vec<i32>) -> i32 {
    // do stuff with v1 and v2
    42
}
let v1 = vec![1, 2, 3];
let v2 = vec![1, 2, 3];
let answer = foo(&v1, &v2);
// this is possible!


// # Borrowing and mutating (&mut) 
let mut x = 5;
{ // <- borrowing needs a scope smaller than that of the owner!
    let y = &mut x;
    *y += 1; // variable y has to be accessed tith *y, because it is mutable reference
}
println!("{}", x); // <- prints 6 (5+1)


// adding an element to a mutable vector:
fn main() {
    let mut a = vec![1,2,3];
    add(&mut a);

    println!("{:?}",a)
}
fn add(v: &mut Vec<i32>) {
    v.push(99);
}

Lifetimes
// # 
// implicit
fn foo(x: &i32) {
}

// explicit
fn bar<'a>(x: &'a i32) { // reads: lifetime a
}

aoc2019's People

Contributors

sonnenhut avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.