These are chat archives for rust-lang/rust

13th
Mar 2018
Niranjan A Kartha
@n-kartha
Mar 13 2018 07:26
Hi
Does anyone know how to set up RustyCode for Visual Studio Code?
Andrey Lesnikov
@ozkriff
Mar 13 2018 07:27
Niranjan A Kartha
@n-kartha
Mar 13 2018 07:27
That keeps crashing
And stops working most of the time
I used to have it, but I uninstalled it because of this
Andrey Lesnikov
@ozkriff
Mar 13 2018 07:32
Some RLS versions are buggy. rustc 1.25.0-nightly (6c04c4103 2018-02-05) works fine for me so far
Niranjan A Kartha
@n-kartha
Mar 13 2018 07:37
How do you fix RLS Starting going on forever?
It happens very often
It just does not start up completely
I have to reload VSCode to fix it
It's annoying
Andrey Lesnikov
@ozkriff
Mar 13 2018 07:38
It just works for me with that version of RLS
Niranjan A Kartha
@n-kartha
Mar 13 2018 07:39
How do I change the version of RLS?
Andrey Lesnikov
@ozkriff
Mar 13 2018 07:40
add "rust-client.channel": "nightly-2018-01-01", with the required channel to the config
(and actually install that channel with rustup install nightly-2014-12-18)
Niranjan A Kartha
@n-kartha
Mar 13 2018 07:42
Okay
Got it
Niranjan A Kartha
@n-kartha
Mar 13 2018 08:22
One more thing
use std::ops::{Add};
use std::io::{Error, ErrorKind};

pub struct Vector<T>
    where T: Add
{
    values: Vec<T>
}

impl<T> Vector<T>
    where T: Add
{
    pub fn new() -> Vector<T> {
        Vector {
            values: Vec::new()
        }
    }
}

impl<T> Add for Vector<T>
    where T: Add
{
    type Output = Result<Vector<<T as Add>::Output>, Error>;

    fn add(self, other: Vector<T>) -> Result<Vector<<T as Add>::Output>, Error> {
        let mut output = Vector::new();

        if self.values.len() != other.values.len() {
            return Err(Error::new(ErrorKind::InvalidInput,
                                String::from("the vectors specified \
                                    are of different lengths")));
        }

        let mut i = 0;
        let mut len = self.values.len();

        while i < len {
            output.values.push(self.values[i] + other.values[i]);
        }

        Ok(output)
    }
}
How do I do that correctly?
I want to add 2 Vectors and return the element-wise sum in another Vector
Michal 'vorner' Vaner
@vorner
Mar 13 2018 08:29
For one, you probably can't define the trait for vectors, because neither the trait nor the type is yours. But if you wanted a function that does that, you probably want to have a look at https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.zip and https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect
Niranjan A Kartha
@n-kartha
Mar 13 2018 08:32
But wait it seems like f64 + f64 is not defined wtf
My main objective is to do it for f64s
Why isn't Add implemented for f64?
Michal 'vorner' Vaner
@vorner
Mar 13 2018 08:34
That doesn't sound correct, what errors do you get? Can you demonstrate it in the playground?
Niranjan A Kartha
@n-kartha
Mar 13 2018 08:34
Okay
PS C:\Users\User\Desktop\niranjan\rust\mimir> cargo build
   Compiling mimir v0.1.0 (file:///C:/Users/User/Desktop/niranjan/rust/mimir)
error[E0277]: the trait bound `f64: std::ops::Add` is not satisfied
  --> src\components\vector.rs:20:11
   |
20 | impl<f64> Add for Vector<f64> {
   |           ^^^ no implementation for `f64 + f64`
   |
   = help: the trait `std::ops::Add` is not implemented for `f64`
   = help: consider adding a `where f64: std::ops::Add` bound
   = note: required by `components::vector::Vector`

error[E0277]: the trait bound `f64: std::ops::Add` is not satisfied
  --> src\components\vector.rs:21:5
   |
21 |     type Output = Result<Vector<f64>, Error>;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `f64 + f64`
   |
   = help: the trait `std::ops::Add` is not implemented for `f64`
   = help: consider adding a `where f64: std::ops::Add` bound
   = note: required by `components::vector::Vector`

error[E0277]: the trait bound `f64: std::ops::Add` is not satisfied
  --> src\components\vector.rs:23:5
   |
23 | /     fn add(self, other: Vector<f64>) -> Result<Vector<f64>, Error> {
24 | |         let mut output = Vector::new();
25 | |
26 | |         if self.values.len() != other.values.len() {
...  |
39 | |         Ok(output)
40 | |     }
   | |_____^ no implementation for `f64 + f64`
   |
   = help: the trait `std::ops::Add` is not implemented for `f64`
   = help: consider adding a `where f64: std::ops::Add` bound
   = note: required by `components::vector::Vector`

error: aborting due to 3 previous errors

error: Could not compile `mimir`.

To learn more, run the command again with --verbose
Edvin Malinovskis
@nCrazed
Mar 13 2018 08:36
@n-kartha what version of rust are you using?
Niranjan A Kartha
@n-kartha
Mar 13 2018 08:37
nightly 1.24
rustc 1.24.0-nightly (9389e23a8 2017-12-31)
I've added a main function and modified the new constructor of Vector so that it is runnable
Niranjan A Kartha
@n-kartha
Mar 13 2018 08:44
Wait I think I get it
Edvin Malinovskis
@nCrazed
Mar 13 2018 08:44
impl<f64> Add for Vector<f64> where f64: std::ops::Add
missing the where
Niranjan A Kartha
@n-kartha
Mar 13 2018 08:44
f64 there is not the generic type
f64 is being taken as a custom type name
Edvin Malinovskis
@nCrazed
Mar 13 2018 08:45
and I think that with that where clause you can replace f64 with T, since anything that implements Add should work there
Niranjan A Kartha
@n-kartha
Mar 13 2018 08:45
Well, but if I do that, I get another error
Wait I'll show it
Niranjan A Kartha
@n-kartha
Mar 13 2018 08:51
Okay I fixed it
Thanks for the help
When I do replace it with T, I have to replace the output type with Result<<T as Add>::Output, Error>
That's what I was doing wrong
Andrii Zymohliad
@azymohliad
Mar 13 2018 10:22
Rust is most-loved technology on SO survey 3rd year in a row :) https://insights.stackoverflow.com/survey/2018
Michal 'vorner' Vaner
@vorner
Mar 13 2018 10:23
Yes, pretty cool result for a language officially 3 years old.
Ben Kimock
@saethlin
Mar 13 2018 14:51
@n-kartha @ozkriff
RLS works on stable now, so you can "rust-client.channel": "stable",. Stable hasn't crashed on me yet.