GithubHelp home page GithubHelp logo

learn-rust-by-building-real-applications's Introduction

learn-rust-by-building-real-applications's People

Contributors

gavadinov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

learn-rust-by-building-real-applications's Issues

QueryString improvements - questions

Hello.

After completing your Rust course, I try to improve QueryString to handle "percent"-encoded keys and values, by using the urlencoding crate. I figured I have to change QueryString and Value to store String objects instead of &str slices, so:

#[derive(Debug)]
pub struct QueryString {
    data: HashMap<String, Value>,
}

#[derive(Debug)]
pub enum Value {
    Single(String),
    Multiple(Vec<String>),
}

Unfortunately, the urlencoding::decode() functions returns Cow<str> instead of normal String or &str ๐Ÿคฏ

How do I "convert" the Cow<str> into String? Should it even be converted? Is the following helper function reasonable ???

use urlencoding::decode;

fn url_decode(str: &str) -> String {
    match decode(str) {
        Ok(decoded) => decoded.into_owned(), // <-- the best way to get String from Cow<str> ???
        Err(_) => str.to_owned(),
    }
}

...or better to store Cow<str> instances in the Value ??? ๐Ÿ˜…


Another problem arises when key and val are of type String now:

impl From<&str> for QueryString {
    fn from(s: &str) -> Self {
        let mut data = HashMap::new();

        for sub_str in s.split('&').filter(|x| !x.is_empty()) {
            let mut parts = sub_str.splitn(2, '=');
            let key = parts.next().map(str::trim).map(url_decode).unwrap_or_default(); // <-- String
            let val = parts.next().map(str::trim).map(url_decode).unwrap_or_default(); // <-- String

            data.entry(key.to_owned())
                .and_modify(|existing| match existing {
                    Value::Single(prev_val) => {
                        *existing = Value::Multiple(vec![prev_val.to_owned(), val]);
                    }
                    Value::Multiple(vec) => vec.push(val),
                })
                .or_insert(Value::Single(val)); // <-- ERROR: use of moved value: `val` โ˜น๏ธ
        }

        QueryString { data }
    }
}

The value val gets moved into the closure (at and_modify()), even though I don't use the move keyword ๐Ÿ˜ข

Is there any way to prevent that? Solution I have found is by using clone() method:

let key = parts.next().map(str::trim).map(url_decode).unwrap_or_default();
let val = parts.next().map(str::trim).map(url_decode).unwrap_or_default();

data.entry(key.to_owned())
    .and_modify(|existing| match existing {
        Value::Single(prev_val) => {
            *existing = Value::Multiple(vec![prev_val.to_owned(), val.clone()]); // <-- clone!
        }
        Value::Multiple(vec) => vec.push(val.clone()), // <-- clone!
    })
    .or_insert(Value::Single(val));

Is this a proper solution, or how do I go about this ???

It feels like there is an unnecessary clone in the and_modify() branch now. That's because if we actually enter the and_modify() branch, then the or_insert() branch will never be entered โ€“ and vice versa. But how do we solve it?

Best regards!

Please share your gdb config

Hi,
by default, gdb does not pretty-print rust structures as on your videos. Can you add info in README.md how to set it up?

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.