Giter Club home page Giter Club logo

learning_rust-lang's Introduction

Rust playground

Install rust:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Update rustup:

rustup update

Check version:

rustc --version

Install C compiler:

sudo apt install build-essential

Build project

cargo new [PROJECT-NAME]

Install Cargo dependencies

cargo fetch

NB. Similar to npm install

Compile main.rs

cargo build --dev

--

Compile and run main.rs

    cargo run --release

Compile and run main.rs with backtrace

    RUST_BACKTRACE=1 cargo run

Can also be run with 1 | full

Rust & Serde

Handling missing JSON fields

#[serde(skip_serializing_if = "Option::is_none")]

(Error handling - Option & unwrap - Unpacking options with ?)[https://doc.rust-lang.org/rust-by-example/error/option_unwrap/question_mark.html]

(Rust: Using Options by example)[https://www.ameyalokare.com/rust/2017/10/23/rust-options.html] (Rust Arrays Tutorial)[https://www.koderhq.com/tutorial/rust/array/] (Deserialize variable meta object with serde)[https://stackoverflow.com/a/61646314]

(Vec & Indexing)[https://doc.rust-lang.org/beta/std/vec/struct.Vec.html#indexing] (Deserialize a JSON string or array of strings into a Vec)[https://www.javaer101.com/en/article/15848189.html] (From Go to Rust: JSON and YAML)[https://dzone.com/articles/from-go-to-rust-json-and-yaml]

Option

(The Option Enum and Its Advantages Over Null Values)[https://doc.rust-lang.org/stable/book/ch06-01-defining-an-enum.html]

Rust books

(Cheat sheets & books)[https://cheats.rs/] (The Rust Programming Language)[file:///home/INSERT-NAME/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/share/doc/rust/html/book/index.html]

Auto genereate structs from JSON

(https://app.quicktype.io/)[https://app.quicktype.io/]

API Request ressources

https://betterprogramming.pub/learn-rust-by-example-structs-methods-impl-and-traits-de11e8d02b68

Tutorials

https://www.educative.io/courses/learn-rust-from-scratch/ https://www.educative.io/courses/ultimate-guide-to-rust-programming

Backup ressources

// https://www.reddit.com/r/rust/comments/7hasv6/mixed_valuestruct_deserialization_with_serde_json/dqpht6v?utm_source=share&utm_medium=web2x&context=3 // https://transform.tools/json-to-rust-serde

// Unwrapping Rust's errors - https://medium.com/swlh/unwrapping-rusts-errors-552e583e2963

Serde - Generiske parametre: https://doc.rust-lang.org/book/ch10-01-syntax.html https://doc.rust-lang.org/beta/rust-by-example/generics/assoc_items/types.html https://serde.rs/ dtolnay/serde-yaml#195 serde-rs/serde#984

https://learning-rust.github.io/docs/e3.option_and_result.html#is-some-is-none-is-ok-is-err

https://www.softax.pl/blog/rust-lang-in-a-nutshell-2-enums-pattern-matching-options/

Initialize HashMap

Tutorial: https://www.koderhq.com/tutorial/rust/hashmap/

use std::collections::HashMap;

fn main() {

let mut language: HashMap<&str, &str> = HashMap::new();

language.insert("Einar", "Norway");
language.insert("Olaf", "Denmark");
language.insert("Harald", "Iceland");

println!("{:?}", language);

}

Struct and HashMap

use std::collections::HashMap;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct User {
    email: Option<String>,
    name: Option<String>,
}

impl User {
    fn new(attributes: &HashMap<String, String>) -> User {
        User {
            email: attributes.get("email").cloned(),
            name: attributes.get("name").cloned(),
        }
    }
}

fn main() {
    let mut map = HashMap::new();
    map.insert(String::from("email"), String::from("[email protected]"));
    map.insert(String::from("name"), String::from("John Doe"));

    let user_model = User::new(&map);
    let p = serde_json::to_string(&user_mode).unwrap();

    println!("{}", p);
}

learning_rust-lang's People

Contributors

rolfmadsen avatar

Watchers

 avatar James Cloos 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.