These are chat archives for rust-lang/rust

14th
Apr 2019
Sam Johnson
@sam0x17
Apr 14 05:01
are there any constraints on T and U I could add that would make this work?
#[cfg(test)]
pub fn assert_iter_eq<T: ExactSizeIterator, U: ExactSizeIterator>(a: T, b: U) {
    assert_eq!(a.len(), b.len());
    for i in 0..a.len() {
        assert_eq!(a[i], b[i]);
    }
}
error[E0608]: cannot index into a value of type `T`
  --> src/util.rs:12:20
   |
12 |         assert_eq!(a[i], b[i]);
   |                    ^^^^
etc..
Sam Johnson
@sam0x17
Apr 14 05:16
haha omg thank you yeah I figured I'd also run into partial equality as well
Sam Johnson
@sam0x17
Apr 14 05:23
hmm how would I extend this to also allow for doing a[i] and b[i] so I can implement assert_eq_ne? I've tried adding Index as a trait bound but it says Index doesn't exist even though that's the name of the standard trait for indexing
*assert_iter_ne
Sam Johnson
@sam0x17
Apr 14 05:45
I did figure out a way using a Vec that isn't as pretty as simply indexing directly into a and b but it works so meh:
pub fn assert_iter_ne<T: ExactSizeIterator, U: ExactSizeIterator>(a: T, b: U)
where
    T: ExactSizeIterator,
    <T as Iterator>::Item: Debug + PartialEq,
    U: ExactSizeIterator + Iterator<Item = <T as Iterator>::Item>
{
    if a.len() != b.len() {
        return;
    }
    let mut items_a = Vec::with_capacity(a.len());
    let a_len = a.len();
    for item in a {
        items_a.push(item);
    }
    let mut same_count = 0;
    let mut i = 0;
    for item_b in b {
        if items_a[i] == item_b {
            same_count += 1;
        }
        i += 1;
    }
    assert!(same_count < a_len);
}
Sam Johnson
@sam0x17
Apr 14 05:59
scratch that, this one that uses zip is better. Thanks @kgv !
pub fn assert_iter_ne<T: ExactSizeIterator, U: ExactSizeIterator>(a: T, b: U)
where
    T: ExactSizeIterator,
    <T as Iterator>::Item: Debug + PartialEq,
    U: ExactSizeIterator + Iterator<Item = <T as Iterator>::Item>
{
    if a.len() != b.len() {
        return;
    }
    let mut same_count = 0;
    let a_len = a.len();
    for (left, right) in a.zip(b) {
        if left == right {
            same_count += 1;
        }
        assert_eq!(left, right);
    }
    assert!(same_count < a_len);
}
(remove that assert_eq!(left, right); line -- missed that)
Chris Pearce
@cjpearce
Apr 14 10:07

Hi everyone,
I'm a beginner and I have a type inference question that I'd like to understand a little more about, and would really appreciate it if there's anyone that could point me in the right direction to learn more.

type StringErr = Result<u32, String>;
type SliceErr = Result<u32, &'static str>;

fn converts_to_err(opt : Option<u32>) -> StringErr {
    // In the line below, through what method is the compiler able to convert
    // my Result<u32, &'static str> to the Result<u32, String> required for '?'
    let value = opt.ok_or_else(|| "some static slice")?;

    Ok(value)
}

fn main() {
    let works : StringErr = converts_to_err(None); // <- works just fine

    // It mainly confuses me because I'm unable to do the same thing manually
    let slice_err : SliceErr = Ok(2);
    // let c : StringErr = StringErr::from(slice_err); // <-- doesn't compile
}

This may be a fundamental misunderstanding, but I don't understand how the ok_or_else should be able to work if the explicit from doesn't work.

octave99
@octave99
Apr 14 11:08
@cjpearce I believe the first conversion is due to impl<'a> From<&'a str> for String (https://doc.rust-lang.org/src/alloc/string.rs.html#2175-2180) and there is no such trait impl From<SliceErr> for StringErr
Chris Pearce
@cjpearce
Apr 14 11:30
Ok I see - I was wondering if it that was the case. So I'm guessing that if I was able to look into the implementation of ?, I'd find it uses From trait bounds on the type parameters of the Result type to do the conversion, rather than just on the Result type itself. Thank you! I might have a look to see if I can find where this happens.
Chris Pearce
@cjpearce
Apr 14 11:41
It was far easier to find it in the source than I expected, and far easier to understand too. Now I know to look in the source first.
https://doc.rust-lang.org/src/core/macros.rs.html#306-314
David Vo
@auscompgeek
Apr 14 11:50
The ? operator doesn't use try!
Chris Pearce
@cjpearce
Apr 14 11:57
@auscompgeek my mistake, I must have misinterpreted something I read. Do you know where I can look to find ? specifically?
David Vo
@auscompgeek
Apr 14 12:05
It'd be implemented where the rest of the operators are
Chris Pearce
@cjpearce
Apr 14 12:09
Thanks, I can see there's a Try trait in std::ops that looks like it's what I was looking for. I'll have to read up a bit more to understand what's going on.
Frederic
@FredericChang
Apr 14 12:40
Is it possible to install two diffierent version in Windows system? one is stable two is nightly
Peter Atashian
@retep998
Apr 14 12:40
@FredericChang That is literally what rustup is for
it lets you install multiple rust toolchains and switch between them easily
Frederic
@FredericChang
Apr 14 12:42
Because i saw in CLion when we are running main.rs, we can select different
@retep998 do you know how to install plug-in in windows?
Peter Atashian
@retep998
Apr 14 12:43
Plug-in?
Frederic
@FredericChang
Apr 14 12:43
like
sudo add-apt-repository -y ppa:zoogie/sdl2-snapshots
Peter Atashian
@retep998
Apr 14 12:44
???
because i wanna learn from this project
Peter Atashian
@retep998
Apr 14 12:47
on windows the sdl2 crate should be able to build SDL2 from source automatically
Frederic
@FredericChang
Apr 14 12:49
you mean when i run main.rs, sdl2 will be build automactically?
Peter Atashian
@retep998
Apr 14 12:49
if the bundled feature of sdl2-sys is enabled that is
but typically on windows you would just go to the sdl2 website, download the prebuilt library, extract it somewhere, and set a few environment variables
Frederic
@FredericChang
Apr 14 12:51
Ok, i will check it tomorrow. thanks.
Peter Atashian
@retep998
Apr 14 12:53
if you're using pc-windows-msvc, you'd add the directory containing sdl2.lib to LIB and the directory containing sdl2.dll to PATH
Aleksandr Lykhouzov
@lykhouzov
Apr 14 23:48

Hello. here is a code

let mut drag = Rc::new(false);
let mousedown_cb =
        Closure::wrap(Box::new(|_event: MouseEvent| {
            *Rc::get_mut(&mut drag).unwrap() = true;
        }) as Box<dyn FnMut(MouseEvent)>);
    event_target
        .add_event_listener_with_callback("mousedown", mousedown_cb.as_ref().unchecked_ref())
        .unwrap();
    mousedown_cb.forget();

I am trying to mutate kind of global variable and rust says me that drag does not live long enough.
And i cannot get how I could get it works.
Could someone help me to understand this, or maybe i need to re-think whole logic at all
Thank you