These are chat archives for rust-lang/rust

12th
May 2019
brianwk
@brianwk
May 12 01:13
hi, I'm having trouble figuring out how to get something like this to work in Rust: https://gist.github.com/rust-play/bdf56f6d35679e3beadf98c5cdf09b46
Ghost
@ghost~5cd49501d73408ce4fbfdd2f
May 12 03:25
Why rust language do not have class and inheritance
Is it because of difficulty to maintain scope
chisana
@chisa0a
May 12 03:52
@ebinshabu different model from Object Oriented programming. rust has structs (just field members), traits (for inheritance / generic programming), and impls (how you do methods / member functions)
scope is still maintained in Rust
so is access control, at the function, struct and module levels
^ loose description, the Rust Book describes it better
Siddhant Sanyam
@siddhant3s
May 12 04:23
I'm confused about traits which define only one method. Are they in any way special such that all closure with a single method can be passed in place of them?
For example: The documentation of Iron::new says that it needs a Handler. However, it seems we can simply pass a closure or a function as described in the hello-world program.
Please note that I'm very new to Rust.
I'm wondering how does that work?
chisana
@chisa0a
May 12 04:28
@siddhant3s: iiuc structs implementing the Handler trait need to implement Send + Sync traits, and have 'static lifetime. the struct must also implement the handle function with the given signature
Siddhant Sanyam
@siddhant3s
May 12 04:30
Yes, which means that the only thing which I should be able to pass to Iron::new should be something which implements Handler. How come I'm able to pass a simple lambda (closure) to Iron::new as described in the hello-world example?
chisana
@chisa0a
May 12 04:30
Send and Sync have nice sugar syntax for their impl, can't remember atm what the exact rules are, but they are easily searchable. remember seeing good answers on stackoverflow/stackexchange, and the Rust Book has good sections on traits
the lambda may meet all the above requirements
Siddhant Sanyam
@siddhant3s
May 12 04:31
I think it's because of this
chisana
@chisa0a
May 12 04:31
don't know if Rust uses an anonymous struct, or some other magic
Siddhant Sanyam
@siddhant3s
May 12 04:31
They have implemented it on F:
impl<F> Handler for F where
    F: Send + Sync + 'static + Fn(&mut Request) -> IronResult<Response>,
chisana
@chisa0a
May 12 04:32
:) looks right to me
Siddhant Sanyam
@siddhant3s
May 12 04:32
Which means anything which implements Fn + Send + Sync + 'static should be able to act as a Handler.
Yeah, I think everything is cool now.
chisana
@chisa0a
May 12 04:34
:+1:
Siddhant Sanyam
@siddhant3s
May 12 04:34
Thank you
chisana
@chisa0a
May 12 04:35
You're welcome
Siddhant Sanyam
@siddhant3s
May 12 04:37
So well, this is sort of emulating Function overloading. Noice.
chisana
@chisa0a
May 12 04:38
yeah, took me a while to get traits (still getting). they are super nice
chisana
@chisa0a
May 12 04:49
function overloading, template programming, inheritance, and borrow-checking all rolled into one language primitive. rustlang devs are very slick
brianwk
@brianwk
May 12 06:59
Why does this not work?
impl Order {
    pub fn all(db: PitchDb) -> Vec<Order> {
        let (orders, submissions) = orders::table
            .left_join(submissions::table)
            .load(&*db)
            .into_iter()
            .unzip();

        return orders.collect();
    }
}
Omer Katz
@thedrow
May 12 11:03
Is there a nice party trick to verify that four out of five Option items in a tuple are None?
Omer Katz
@thedrow
May 12 11:32

I tried the following:

if (hex, bytes, bytes_le, fields, int).fold(0, |c, arg| {
                if let Some(a) = arg {
                    c
                } else {
                    c + 1
                }
            }) != 4
            {
                return TypeError::into(
                    "one of the hex, bytes, bytes_le, fields, or int arguments must be given",
                );
            }

I got this error:

error[E0599]: no method named `fold` found for type `(std::option::Option<&str>, std::option::Option<pyo3::Py<pyo3::types::PyBytes>>, std::option::Option<pyo3::Py<pyo3::types::PyBytes>>, std::option::Option<pyo3::Py<pyo3::types::PyTuple>>, std::option::Option<u64>)` in the current scope
  |
  = note: the method `fold` exists but the following trait bounds were not satisfied:
          `&mut (std::option::Option<&str>, std::option::Option<pyo3::Py<pyo3::types::PyBytes>>, std::option::Option<pyo3::Py<pyo3::types::PyBytes>>, std::option::Option<pyo3::Py<pyo3::types::PyTuple>>, std::option::Option<u64>) : std::iter::Iterator`

Seems like I can't iterate this tuple but I don't know why

Marcin Kumorek
@krnik
May 12 14:16

I'm no pro but I can't see Iterator implementation for tuple.
So probably you'd be better of using vectors/arrays.

vec![hex, bytes, bytes_le, fields, int].fold(...)

Denis Lisov
@tanriol
May 12 14:45
@thedrow Are you sure you don't want to use an enum instead of five Options?
David Harvey-Macaulay
@alteous
May 12 16:52
Hi, is there a generally accepted meaning for the 'author' section in Cargo.toml? The cargo documentation says it's open to interpretation: https://doc.rust-lang.org/cargo/reference/manifest.html#the-authors-field-optional