These are chat archives for rust-lang/rust

24th
Feb 2018
Philippe SEGATORI
@tigitz
Feb 24 2018 01:04 UTC
Hi, any experienced rust developer would mind sharing the best way to quickly edit external crate source for the main project I work on ? Can I run cargo run with the edited source in ".cargo/registry/src/github.com-1ecc6299db9ec823/ispell-0.3.0/src/spell_checker.rs" for example ?
Andrey Lesnikov
@ozkriff
Feb 24 2018 05:01 UTC
clone crate's repo, edit it and add its path to https://doc.rust-lang.org/cargo/reference/manifest.html#the-patch-section
Michal 'vorner' Vaner
@vorner
Feb 24 2018 07:45 UTC
@tigitz I usually fork it on github, create a branch and all that, and reference it through crate = { git = "https://my-repo", branch = "something" } and then create a merge request. Or just report the problem and do something else in the meantime ‒ some crate authors are super fast and often a fixed version gets published in less than a day.
Michal 'vorner' Vaner
@vorner
Feb 24 2018 08:01 UTC
@spacekookie I think the default compare will be as fast as possible in given circumstances, so it'll short-circuit if it finds them different. It is done by comparing byte slices and I can't find that in the standard library :-|.
Which brings me to question, this doesn't seem to be unicode-aware compare.
Roman Frołow
@rofrol
Feb 24 2018 12:28 UTC

hello
I need help with juniper
I've copied this file https://github.com/graphql-rust/juniper/blob/master/juniper/src/tests/model.rs
to my project
But when I cargo run, I get:

error[E0432]: unresolved import `InputValue`
--> src/model.rs:5:10
|
5 | #[derive(GraphQLEnum, Copy, Clone, Eq, PartialEq, Debug)]
|          ^^^^^^^^^^^ no `InputValue` in the root

I added InputValue to main.rs, then compiler wanted another uses. So even with this in main.rs:

use juniper::InputValue;
use juniper::Value;
use juniper::ToInputValue;
use juniper::FromInputValue;
use juniper::Executor;

I got:

error[E0365]: `InputValue` is private, and cannot be reexported
--> src/model.rs:5:10
|
5 | #[derive(GraphQLEnum, Copy, Clone, Eq, PartialEq, Debug)]
|          ^^^^^^^^^^^ reexport of private `InputValue`
|
= note: consider declaring type or module `InputValue` with `pub`

error[E0365]: `Value` is private, and cannot be reexported
--> src/model.rs:5:10
|
5 | #[derive(GraphQLEnum, Copy, Clone, Eq, PartialEq, Debug)]
|          ^^^^^^^^^^^ reexport of private `Value`
|
= note: consider declaring type or module `Value` with `pub`

and so on...

Dylan DPC
@Dylan-DPC
Feb 24 2018 12:43 UTC
better to create an issue on the repo. You will get faster answers :)
Roman Frołow
@rofrol
Feb 24 2018 12:43 UTC
nice idea
gnzlbg
@gnzlbg
Feb 24 2018 15:45 UTC
How can I associate a lifetime with a return type ?
fn bar<'a>(y: &'a i32) -> impl FnMut()->i32 does not work
because the returned closure captures y
Restioson
@Restioson
Feb 24 2018 15:46 UTC
try (impl FnMut() -> i32) + 'a?
gnzlbg
@gnzlbg
Feb 24 2018 15:46 UTC
error: expecting a path
I've tried with impl 'a + FnMut() -> i32 too without luck
Restioson
@Restioson
Feb 24 2018 15:47 UTC
try impl FnMut() -> i32 + 'a
This message was deleted
nah idk
try that
gnzlbg
@gnzlbg
Feb 24 2018 15:47 UTC
that says that the returned value requires the static lifetime
Restioson
@Restioson
Feb 24 2018 15:48 UTC
:thinking:
gnzlbg
@gnzlbg
Feb 24 2018 15:48 UTC
if I change the return type to &'a i32
then it works
Restioson
@Restioson
Feb 24 2018 15:48 UTC
hm
gnzlbg
@gnzlbg
Feb 24 2018 15:48 UTC
but if I want to capture a reference
and return a value
Restioson
@Restioson
Feb 24 2018 15:48 UTC
impl FnMut() -> (i32 + 'a)
gnzlbg
@gnzlbg
Feb 24 2018 15:48 UTC
that doesn't work either
i32 is not a trait
Restioson
@Restioson
Feb 24 2018 15:49 UTC
ah fuck
ummmmm
wow this has me stumped
Junjie Huang
@huangjj27
Feb 24 2018 15:49 UTC
lifetime param is part of return type, not a trait = =
Restioson
@Restioson
Feb 24 2018 15:49 UTC
send full code on playground so i can play around with it?
Junjie Huang
@huangjj27
Feb 24 2018 15:50 UTC
and it's use on reference to tell the compiler that the references with given lifetime params can be use safe
I basically want to make the returned closure return i32 instead of &i32
Restioson
@Restioson
Feb 24 2018 15:51 UTC
ok so you need to deref it then
gnzlbg
@gnzlbg
Feb 24 2018 15:51 UTC
inside the closure
Restioson
@Restioson
Feb 24 2018 15:51 UTC
a move closure wont be FnMut either
it shouldnt be, at least
Junjie Huang
@huangjj27
Feb 24 2018 15:51 UTC
then why consider the lifetime? i32 is Copy default @gnzlbg
gnzlbg
@gnzlbg
Feb 24 2018 15:51 UTC
because this is a mwe
Restioson
@Restioson
Feb 24 2018 15:51 UTC
mve*
basically youre trying to take &T and coerce it to T
gnzlbg
@gnzlbg
Feb 24 2018 15:52 UTC
no
Restioson
@Restioson
Feb 24 2018 15:52 UTC
to do that you need to deref it
Junjie Huang
@huangjj27
Feb 24 2018 15:52 UTC
what does mve*mean?
gnzlbg
@gnzlbg
Feb 24 2018 15:52 UTC
i am storing a &T in a closure
Restioson
@Restioson
Feb 24 2018 15:52 UTC
well, that's what rust sees
minimum viable example
gnzlbg
@gnzlbg
Feb 24 2018 15:52 UTC
minimum working example
Restioson
@Restioson
Feb 24 2018 15:52 UTC
return *thing, not just thing
mwe
Junjie Huang
@huangjj27
Feb 24 2018 15:52 UTC
get
gnzlbg
@gnzlbg
Feb 24 2018 15:52 UTC
in any case
Restioson
@Restioson
Feb 24 2018 15:52 UTC
i always think of it like mvp, minimum viable product
gnzlbg
@gnzlbg
Feb 24 2018 15:52 UTC
the real object is large
I want to have a closure that stores a reference to it
Restioson
@Restioson
Feb 24 2018 15:53 UTC
yes
gnzlbg
@gnzlbg
Feb 24 2018 15:53 UTC
and that clones it out when needed
Restioson
@Restioson
Feb 24 2018 15:53 UTC
but do you want to return the object or a reference to it?
gnzlbg
@gnzlbg
Feb 24 2018 15:53 UTC
a copy / clone on the object
but i don't want to store that in the closure
I also want full borrowck so I am using rust references and not raw pointers for this (I want to get a compile error when I am trying to build a closure that does not live long enough)
it must be doable because the reference case works
the problem is that when I omit the reference in the return type i cannot tell the compiler what the lifetime of the closure is
Junjie Huang
@huangjj27
Feb 24 2018 15:55 UTC
so you should wrap it in an uncopiable type?
gnzlbg
@gnzlbg
Feb 24 2018 15:56 UTC
fn bar<'a>(y: &'a i32) -> impl (FnMut()->i32) + 'a {
move || *y
}
this works :/
Restioson
@Restioson
Feb 24 2018 15:56 UTC
i think you need for
gnzlbg
@gnzlbg
Feb 24 2018 15:56 UTC
I tried (impl FnMut()->i32) + 'a
but never tried impl (FnMut()->i32) + 'a
duh
Restioson
@Restioson
Feb 24 2018 15:57 UTC
try for<'b> somewhere
gnzlbg
@gnzlbg
Feb 24 2018 15:57 UTC
no need
that just works
one just needed to get the parentheses right
thanks anyways, unnecessary head smashing :D
Restioson
@Restioson
Feb 24 2018 15:58 UTC
glad it works!
Junjie Huang
@huangjj27
Feb 24 2018 15:59 UTC

fn bar<'a>(y: &'a i32) -> impl FnMut()->i32 + 'a {
    move || *y
}

fn main() {
    let x = 42;
    println!("{}", bar(&x)());
}
no need to add parentheses 0. 0
Restioson
@Restioson
Feb 24 2018 16:00 UTC
its clearer with parentheses
showing its not i32 + 'a
or maybe
thats what you need
well idk
Junjie Huang
@huangjj27
Feb 24 2018 16:00 UTC
ok
I'm going to write lifetime params in the first place for more clearness
Junjie Huang
@huangjj27
Feb 24 2018 16:34 UTC
like impl 'a + FnMut() -> i32
but this only means I return a closure with liftetime 'a, not only the y with the 'a lifetime
gnzlbg
@gnzlbg
Feb 24 2018 16:34 UTC
yeah
my example stores the y in the closure
so if your closure only has one data member, of lifetime 'a, then saying that the closure also has the lifetime 'a is a good approximation
(not a good approximation, but actually the right one)
if your closure stores multiple data-members with different lifetimes then this gets more complicated
Junjie Huang
@huangjj27
Feb 24 2018 16:38 UTC
I think this example can be induced by the compiler.
And I agree you on the more complex cases you're talking about.
let induced = inferred;
Adel Prokurov
@aprokurov
Feb 24 2018 20:17 UTC
Guys, how i can insert here: let text_area: gtk::TextView = builder.get_object("output_label").unwrap() output of let mut out = 1 + 2 for example?
Dylan DPC
@Dylan-DPC
Feb 24 2018 20:19 UTC
didn't understand your question
Adel Prokurov
@aprokurov
Feb 24 2018 20:19 UTC
I have gtk::TextView, and i want to create something like calculator
How to insert to gtk::TextView some strings?
or i32 with f64
Denis Lisov
@tanriol
Feb 24 2018 20:22 UTC
@aprokurov I'd probably start with a gtk::Label instead of gtk::TextView
Adel Prokurov
@aprokurov
Feb 24 2018 20:22 UTC
And how to insert string to gtk::Label?
Denis Lisov
@tanriol
Feb 24 2018 20:24 UTC
I'm pretty sure it should be something like output_label.set_label(&format!("{}", out))
Adel Prokurov
@aprokurov
Feb 24 2018 20:26 UTC
i don't see set_label here:( http://gtk-rs.org/docs/gtk/struct.Label.html
Only this:
use gtk::Label;
    use gtk::prelude::*;

    let label = Label::new(None);
    let text =
r#"Go to the
 <a href="http://www.gtk.org" title="&lt;i&gt;Our&lt;/i&gt; website">
 GTK+ website</a> for more..."#;
    label.set_markup(text);
Denis Lisov
@tanriol
Feb 24 2018 20:27 UTC
They live in a separate trait -- LabelExt::set_label
Adel Prokurov
@aprokurov
Feb 24 2018 20:28 UTC
Thanks!So i just need something like this:
label.set_label(&format!("{}", out));
If i had gtk::Entry i need to let mut out = gtkentrylabel.get_text();?
Denis Lisov
@tanriol
Feb 24 2018 20:31 UTC
Most likely you don't need mut
Adel Prokurov
@aprokurov
Feb 24 2018 20:32 UTC
Thanks for help!
Get error...
error[E0423]: expected value, found struct `gtk::Entry`
  --> src/main.rs:19:22
   |
19 |     let text_entry = gtk::Entry = builder.get_object("text_enter").unwrap();
   |                      ^^^^^^^^^^ did you mean `gtk::Entry { /* fields */ }`?
Denis Lisov
@tanriol
Feb 24 2018 20:35 UTC
The first = should have been :, no?
Adel Prokurov
@aprokurov
Feb 24 2018 20:35 UTC
woops
Now it works
Adel Prokurov
@aprokurov
Feb 24 2018 20:41 UTC
Now i had another question, how to convert this let get_text_from_entry = atext_entry.get_text(); to i32
i know only one, it have type std::option::Option<std::string::String>
Denis Lisov
@tanriol
Feb 24 2018 20:43 UTC
let value: i32 = text.unwrap_or("").parse()?;
Adel Prokurov
@aprokurov
Feb 24 2018 20:47 UTC
let get_text_from_entry = atext_entry.get_text();
        let anum: i32 = get_text_from_entry.unwrap_or("").parse()?;
let anum: i32 = get_text_from_entry.unwrap_or("").parse()?;
   |                         ------------------------------------------
   |                         |
   |                         cannot use the `?` operator in a function that returns `()`
   |                         in this macro invocation
Denis Lisov
@tanriol
Feb 24 2018 20:48 UTC
Well, then you need to do something to this error :-) what do you expect to be done when the user types "hello" into an input field?
Adel Prokurov
@aprokurov
Feb 24 2018 20:49 UTC
It breaks
but i think nobody enter words to entry, only numbers
Denis Lisov
@tanriol
Feb 24 2018 20:49 UTC
Users will do every stupid thing you can imagine :-)
Dylan DPC
@Dylan-DPC
Feb 24 2018 20:50 UTC
:laughing:
Adel Prokurov
@aprokurov
Feb 24 2018 20:50 UTC
error[E0308]: mismatched types
  --> src/main.rs:26:55
   |
26 |         let anum: i32 = get_text_from_entry.unwrap_or("").parse()?;
   |                                                       ^^ expected struct `std::string::String`, found reference
   |
   = note: expected type `std::string::String`
              found type `&'static str`
   = help: here are some functions which might fulfill your needs:
           - .escape_debug()
           - .escape_default()
           - .escape_unicode()
           - .to_ascii_lowercase()
           - .to_ascii_uppercase()

error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
  --> src/main.rs:26:25
   |
26 |         let anum: i32 = get_text_from_entry.unwrap_or("").parse()?;
   |                         ------------------------------------------
   |                         |
   |                         cannot use the `?` operator in a function that returns `()`
   |                         in this macro invocation
   |
   = help: the trait `std::ops::Try` is not implemented for `()`
   = note: required by `std::ops::Try::from_erro
Denis Lisov
@tanriol
Feb 24 2018 20:51 UTC
Yes, the unwrap should have been .unwrap_or("".to_string())
Adel Prokurov
@aprokurov
Feb 24 2018 20:52 UTC
still not working
rror[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
  --> src/main.rs:26:25
   |
26 |         let anum: i32 = get_text_from_entry.unwrap_or("".to_string()).parse()?;
   |                         ------------------------------------------------------
   |                         |
   |                         cannot use the `?` operator in a function that returns `()`
   |                         in this macro invocation
   |
Dylan DPC
@Dylan-DPC
Feb 24 2018 20:53 UTC
the error says it all doesn't it?
Denis Lisov
@tanriol
Feb 24 2018 20:54 UTC
@aprokurov I've already asked you: what do you want to do when the user types some text into your input field and presses the button?
Adel Prokurov
@aprokurov
Feb 24 2018 20:54 UTC

that's full code

extern crate gtk;
use gtk::prelude::*;
use gtk::{Label,Button, Window, WindowType};
use gtk::Entry;

//use gtk::LabelExt;


fn main() {
    if gtk::init().is_err() {
    println!("Failed to initialize GTK.");
    return;
    }
    let glade_src = include_str!("ui.glade");
    let builder = gtk::Builder::new_from_string(glade_src);
    let button: gtk::Button = builder.get_object("proceed").unwrap();
    let window: gtk::Window = builder.get_object("window1").unwrap();
    let text_area: gtk::Label = builder.get_object("output_label").unwrap();
    let atext_entry: gtk::Entry = builder.get_object("anum").unwrap();
    let btext_entry: gtk::Entry = builder.get_object("bnu").unwrap();
    let ctext_entry: gtk::Entry = builder.get_object("cnum").unwrap();

    button.connect_clicked(move |_| {
        println!("Button pressed");
        let get_text_from_entry = atext_entry.get_text();
        let anum: i32 = get_text_from_entry.unwrap_or("".to_string()).parse()?;

    });
    window.show_all();
    gtk::main();
}

That's full error

error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
  --> src/main.rs:26:25
   |
26 |         let anum: i32 = get_text_from_entry.unwrap_or("".to_string()).parse()?;
   |                         ------------------------------------------------------
   |                         |
   |                         cannot use the `?` operator in a function that returns `()`
   |                         in this macro invocation
   |
   = help: the trait `std::ops::Try` is not implemented for `()`
   = note: required by `std::ops::Try::from_error`

error: aborting due to previous error

error: Could not compile `runo_gtk`.
Dylan DPC
@Dylan-DPC
Feb 24 2018 20:54 UTC
well you are already unwrapping it right?
Adel Prokurov
@aprokurov
Feb 24 2018 20:54 UTC
i want to convert it to i32
Dylan DPC
@Dylan-DPC
Feb 24 2018 20:54 UTC
why do you expect it to return a result after unwrapping?
Adel Prokurov
@aprokurov
Feb 24 2018 20:55 UTC
i need to get i32
Denis Lisov
@tanriol
Feb 24 2018 20:55 UTC
And if it's not an i32, then?
Yes, Rust is forcing you to think about this case too.
Adel Prokurov
@aprokurov
Feb 24 2018 20:55 UTC
display error
i make some gtk popup window with error later
Denis Lisov
@tanriol
Feb 24 2018 20:57 UTC
BTW, I'd really suggest you don't throw unwraps right and left with such ease.
Dylan DPC
@Dylan-DPC
Feb 24 2018 20:58 UTC
i'd agree with that
Adel Prokurov
@aprokurov
Feb 24 2018 20:59 UTC
i just need to convert to i32, i think that programm will be used by 4-10 people from my school class
I think better ask about that in gitter channel of gtk-rs
Dylan DPC
@Dylan-DPC
Feb 24 2018 21:02 UTC
i don't see it as a gtk issue tbh
well you are not understanding the code you are writing and the error that is being thrown
Denis Lisov
@tanriol
Feb 24 2018 21:05 UTC
Your error is not gtk-specific. It's related to the fact that you need to process the error case in some way. The ? operator is the most ergonomic one usually, but it needs the caller to expect a Result.
Adel Prokurov
@aprokurov
Feb 24 2018 21:06 UTC
And how i can "create" that caller, i'm not so good at Rust
Denis Lisov
@tanriol
Feb 24 2018 21:06 UTC
What I'd do here is to move the computation to a function returning a Result<i32, whatever_error_type_you_need> and wrap it in a closure that does something to the result.
Adel Prokurov
@aprokurov
Feb 24 2018 21:07 UTC
whatever_error is important?
Denis Lisov
@tanriol
Feb 24 2018 21:09 UTC
If the only operation that can fail is parse of an integer, it can be ParseIntError
Adel Prokurov
@aprokurov
Feb 24 2018 21:09 UTC
i think i need to slep, i from Russia and there time is 1 pm, maybe at the morning i can do something with that