These are chat archives for rust-lang/rust

13th
Feb 2018
kriffo
@kriffo
Feb 13 2018 01:57
is there any way to do this?
    let mut s = String::from("hello");
    &mut s[..] = "gooby";
Joonas Koivunen
@koivunej
Feb 13 2018 08:39
@kriffo i would had assumed there was something like that which would work. i guess you could write it character at a time but ... how about s.clear(); s.push_str("gooby");? I think that'd be pretty much equivalent in many ways to what you were looking for
Daniel Bischof
@dbischof90
Feb 13 2018 10:25
For people interested, I got a little workaround with the dynamic match - something as easy as a HashMap :P
Ingvar Stepanyan
@RReverser
Feb 13 2018 11:42
@kriffo Why not do s = String::from("gooby");? You anyway can't rely on pointer to the beginning of the string to remain the same after reallocation (unless length of second string is guaranteed to be <= first one)
clear & push_str might be a little bit more efficient, but allocator is pretty good with reusing memory anyway
and this way code might be slightly more clear
Denis Lisov
@tanriol
Feb 13 2018 18:35
Am I the only one rather frequently missing a method that could be used in roughly the following way?
fn smth() -> Result<String, Error> {
    some_value
        // ...here were some combinators resulting in a `String`...
        .map_self(Ok)
}
Daniel Bischof
@dbischof90
Feb 13 2018 18:36
Take a look at https://github.com/DanielKeep/rust-boolinator/blob/master/README.md such a thing exists at least for booleans :)
Very simple but nice trait
Denis Lisov
@tanriol
Feb 13 2018 18:43
Hm... looks interesting, thank you, but solves a slightly different problem. My usual problem is that I have some chain of combinators in the function body, but the resulting value has to be Ok-wrapped, which would look much better as a final combinator than "oh, I've forgotten to write Ok( in the beginning of the function, let's go there and reinsert it"... and probably would be more readable too.
red75prime
@red75prime
Feb 13 2018 18:44
@tanriol
trait MapAll: Sized {
    fn map_self<U, F>(self, f: F) -> U 
    where F: FnOnce(Self) -> U
    {
        f(self)
    }    
}

impl<T: Sized> MapAll for T {}
Denis Lisov
@tanriol
Feb 13 2018 18:46
@red75prime Yeah, I'm just wondering whether this one is worth a crate.
red75prime
@red75prime
Feb 13 2018 18:49
Probably not. Too trivial.
Dylan DPC
@Dylan-DPC
Feb 13 2018 19:16

Hey. I am using http and calling an API using:
let response = Request::get( path.as_str()).body(());
the URL is valid and when I test it in browser i get the entire response in JSON but when I run my code I get:

Ok(Request { method: GET, uri: /the-url/, version: HTTP/1.1, headers: {}, body: () })
()

what am I missing?

Ingvar Stepanyan
@RReverser
Feb 13 2018 19:19
well it prints the request you constructed as it should
now you need to actually send it somewhere and get Response
Which crate are you using?
Dylan DPC
@Dylan-DPC
Feb 13 2018 19:20
http
I don't see anything mentioned in the docs though
Ingvar Stepanyan
@RReverser
Feb 13 2018 19:21
Well it's not a client
As it says, it's just a collection of common types (for other crates)
You might want to check out hyper instead https://hyper.rs/guides/client/basic/
Dylan DPC
@Dylan-DPC
Feb 13 2018 19:24
ah damn
i thought it was a full client
i might just go back to reqwest which uses hyper
thanks @RReverser
Sean Perry
@shaleh
Feb 13 2018 21:38
I would like to declare a Struct which contains an iterator for a Parser I am working on. But I cannot seem to get the type right. The compiler claims that Sized is not defined.
A pointer to an example in a crate would be great
Denis Lisov
@tanriol
Feb 13 2018 21:40
@shaleh use std::marker::Sized;
Sean Perry
@shaleh
Feb 13 2018 21:41
@tanriol I have a line like this: pub it: Iterator<Item=Something> where does the Sized fit in?
oh oh
I see
Denis Lisov
@tanriol
Feb 13 2018 21:43
Could you show a bigger example of the code you're having problems with?
Sean Perry
@shaleh
Feb 13 2018 21:43
The compiler complaining because a type is not imported is still new to me
The Rust compiler gives better errors everywhere else
nope, just importing Sized did not change anything
drat
@tanriol It is just 'struct Foo { pub it: Iterator<Item=Something>, }when I make aFoo` I am passing an iterator.
but the compiler complains about the struct
Denis Lisov
@tanriol
Feb 13 2018 21:48
If you don't know the exact type of the iterator, it should be either a type parameter, or boxed.
kriffo
@kriffo
Feb 13 2018 22:44
@RReverser @koivunej i wasn't completely clear -- i was specifically trying to take a mutable string, and modify it by slice
so, for example, take "hello" and turn it into "hAAlo" in one line
Denis Lisov
@tanriol
Feb 13 2018 22:45
@kriffo Pretty sure it has to be unsafe.
kriffo
@kriffo
Feb 13 2018 22:45

what I think the problem with that is, is that when I do

let s = String::from("hello")

then s[..] refers to the "hello" in the program text

so it has to be immutable
Denis Lisov
@tanriol
Feb 13 2018 22:46
No, the string is copied.
kriffo
@kriffo
Feb 13 2018 22:46
oh, ok
Denis Lisov
@tanriol
Feb 13 2018 22:49
What the problem really is is that Rust needs to be sure that the string after your manipulation is still a valid UTF-8 string.
How do you expect your code to work? Check that the string lengths in bytes are equal and panic if not?
kriffo
@kriffo
Feb 13 2018 22:59
i was hoping it would :)
i guess not though
error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
  --> main.rs:10:5
   |
10 |     *s = a[..]
   |     ^^ `str` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `str`
   = note: the left-hand-side of an assignment must have a statically known size
Denis Lisov
@tanriol
Feb 13 2018 23:00
What's the problem you're trying to solve in this way?
kriffo
@kriffo
Feb 13 2018 23:04
just messing around...but hypothetically someday i might want to modify a chunk of data
so, for example, if I wanted to write a kind of sed-like function that modifies in-place
"dog dog cat dog dog"...how would I change "cat" to "dog"? how would I delete "cat"?
Denis Lisov
@tanriol
Feb 13 2018 23:06
I'd suggest that specializing it for the unlikely case of "replacement has exactly as many bytes as the source" is most likely not worth it...
kriffo
@kriffo
Feb 13 2018 23:08
that's reasonable
Denis Lisov
@tanriol
Feb 13 2018 23:09
...especially taking into account the fact that any len-changing modification will force you to copy everything after it anyway until the next checkpoint (end-of-line or whatever).