These are chat archives for rust-lang/rust

20th
Nov 2015
Swoorup Joshi
@Swoorup
Nov 20 2015 06:52
eudyptula-challenge.org
Alex Zolotko
@azolotko
Nov 20 2015 15:36
Sorry for a stupid question, but why doesn't std have an impl<'a> Add for &'a str?
Kevin K.
@kbknapp
Nov 20 2015 15:48
I can't say for sure, but I'm guessing because &str is just a view (ptr + len) into a heap allocated String (which is itself just a Vec<u8> that's guaranteed to be valid UTF-8). So there isn't a good way to add two &strs together without just allocating a new String...in which case you could just format!("{}{}", str1, str2)
Erik Hedvall
@Ogeon
Nov 20 2015 15:52
There is also a bit of a divided opinion on whether it's a good idea to implement Add for strings, vector and other collections. That rises the general threshold for something like this to be added.
Alex Zolotko
@azolotko
Nov 20 2015 15:56
@kbknapp Yes, it definitely needs to allocate a new String, but format!("{}{}", str1, str2) doesn't look more idiomatic than str1.to_string() + str2 (to me at least :) ).
Kevin K.
@kbknapp
Nov 20 2015 16:01
true, and there is also String::push_str I believe, but more what I was getting at was there isn't a way to add to &strs and return a &str ;)
Alex Zolotko
@azolotko
Nov 20 2015 16:05
Yeah, but Add has an Output type member, so I assume it doesn't have to return &str.
type Output = String should work, shouldn't it?
Kevin K.
@kbknapp
Nov 20 2015 16:15
it would - but I think some believe that would go against the being explicit about costs? I couldn't really answer :P
mhsjlw
@mhsjlw
Nov 20 2015 16:30
Does anyone know of any libraries for parsing HTML?
Erik Hedvall
@Ogeon
Nov 20 2015 16:31
html5ever is quite good
mhsjlw
@mhsjlw
Nov 20 2015 16:32
I found it on GitHub and brought up the documentation
thanks
are there any examples that I can look at? So that I can get to know how to use it?
I think I found some... but they don't do what I want to do. Thanks anyway though. I'll try it out
Erik Hedvall
@Ogeon
Nov 20 2015 16:35
It depends on how you want to parse it and what you want to do...
mhsjlw
@mhsjlw
Nov 20 2015 16:35
I'd just like to look for tags in html and get the text out of them.
much like jQuery's $('#id').text()
Erik Hedvall
@Ogeon
Nov 20 2015 16:37
Ok. html5ever may be a bit too low level, then. There was something else... one moment.
Never mind, I thought that Kuchiki was something like that, but I remembered it wrong.
mhsjlw
@mhsjlw
Nov 20 2015 16:40
It's OK, I could just use regex...
Erik Hedvall
@Ogeon
Nov 20 2015 16:44
Oh, wait! Look what I found :smile: https://crates.io/crates/select
mhsjlw
@mhsjlw
Nov 20 2015 16:45
Awesome! Thank you, I'll test it out
Also, how do you recommend I retrieve HTML from a website? I may need to pass certain headers too.
I was looking at curl
but maybe there is something you'd recommend.
Kevin K.
@kbknapp
Nov 20 2015 16:47
Hyper should be able to do that last part, at least that's what I've used before
Erik Hedvall
@Ogeon
Nov 20 2015 16:47
You could also look at Hyper. I haven't used its client API, but it's worth a try.
mhsjlw
@mhsjlw
Nov 20 2015 16:48
Perfect! Thanks, guys, I'm really glad I chose to look at Rust. So far, I'm liking it :)
panicbit
@panicbit
Nov 20 2015 23:39
mhsjlw: The select crate provides a comfortable way to do what you want https://crates.io/crates/select
mhsjlw: It provides jQuery/jsoup style selecting of DOM nodes
Oh, I overlooked Ogeon's post, nvm :)
mhsjlw: Also, see this famous stackoverflow answer about parsing html using regex: https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
mhsjlw
@mhsjlw
Nov 20 2015 23:57
@panicbit lol
panicbit
@panicbit
Nov 20 2015 23:58
:)