gavadinov / learn-rust-by-building-real-applications Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
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!
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.