Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    gondolyr
    @gondolyr:matrix.org
    [m]
    p00f
    @chda:matrix.org
    [m]
    Perfect, thanks
    Giuseppe Cannella
    @gekomad
    hi all, I need to copy arr2 to arr1 starting from second position. Im my example I used memcpy but how can I rewrite in Rust code?
    
    let mut arr1 = [1, 2, 3];
    let arr2 = [10, 20];
    memcpy(arr1 + 1, arr2, arr2.len()); // arr1 = 1,10,20
    1 reply
    jplatte
    @jplatte:flipdot.org
    [m]
    arr1[1..].copy_from_slice(&arr2)
    Giuseppe Cannella
    @gekomad
    thanks @jplatte:flipdot.org but in copy_from_slice The length of src must be the same as self
    2 replies
    jplatte
    @jplatte:flipdot.org
    [m]
    E.g. arr1[1..1 + arr2.len()].copy_from_slice (&arr2[..arr1.len()]) for ignoring any elements of arr2 that don't fit, and possibly keep extra elements in arr1 that existed before
    magnus
    @magnusja
    error: can't use a procedural macro from the same crate that defines it
       --> epibator/src/db/off_the_shelf_part.rs:179:5
        |
    179 |     FieldCount,
        |     ^^^^^^^^^^
    What the actual fuck is this about?!
    tanriol
    @tanriol:matrix.org
    [m]
    @magnusja: It's really hard to tell you more given just one line of code. The compiler tells you that you cannot define a proc macro and use it in the same crate. What is FieldCount?
    Giuseppe Cannella
    @gekomad

    Hi all, calling go::<1>() prints 1 1 1....

    1. fn go<const N: usize>() {
    2.    println!("{}", N);
    3.    go::<N>();
    4. }

    but changing line 3 in go::<N^1>() I get an error, how can I do?

    xiretza
    @xiretza:xiretza.xyz
    [m]
    why do you need this?
    Giuseppe Cannella
    @gekomad

    I cand do

    fn go<const N: usize>() {
        println!("{}", N);
        if N == 1 {
            go::<0>();
        } else {
            go::<1>();
        }
    }

    using const I can remove many if at compile time

    xiretza
    @xiretza:xiretza.xyz
    [m]
    sure, but what is the actual problem you're trying to solve? surely you don't need a function that just prints its sole generic argument
    Giuseppe Cannella
    @gekomad
    I've been rewriting in rust my c++ chess engine, you can view the const template at line 260 https://github.com/gekomad/Cinnamon/blob/master/src/Search.cpp
    xiretza
    @xiretza:xiretza.xyz
    [m]
    what's the definition of the X macro?
    Giuseppe Cannella
    @gekomad
    simple #define X(side) ((side)^1) WHITE become BLACK and viceversa
    xiretza
    @xiretza:xiretza.xyz
    [m]
    ah, alright
    does this really need to be a generic though? I can't imagine the performance impact of one additional 1-bit register to be that big
    Giuseppe Cannella
    @gekomad
    little, in c++ is simple but in rust i should duplicate the call if N == 1 ... I think I'll use a simple variable
    ✭ THE BLACK UNICORN ✭
    @iamtheblackunicorn
    Hello! :) I've been coding for 10 years and I am fluent in Python and Dart and got the bright idea to try and learn Rust now! Any advice/tips, considering my background? (I'm 22 btw.).🙈🖤🦄
    MrAzteca
    @azteca1998

    Hello, and welcome to Rust! I guess since you already know how to code you could start with the "rust by example" series (https://doc.rust-lang.org/stable/rust-by-example). Don't forget to check out the book for details and more extensive documentation (https://doc.rust-lang.org/book/) and the Rust standard documentation (doc.rust-lang.org/std)

    I guess since you come from garbage collected languages, you should learn how non-garbage-collected languages work (Rust, C, C++...), what are some of the issues with them and how they are addressed in Rust (I'm thinking about the borrow checker, mutability, lifetimes...). You'll learn that Rust's approach to those problems is quite interesting albeit a bit complicated for new users; but I still recommend learning it because it will be useful for not making mistakes on other languages.

    Good luck, and don't hesitate to ask anything!

    ✭ THE BLACK UNICORN ✭
    @iamtheblackunicorn
    @azteca1998 Wow, thank you most kindly for such a warm welcome! I will most definitely have a look at these links, I already flew over the Rust book yesterday! I am quite glad that everything is so well documented, makes picking up the syntax much easier! While I do understand that you would advise me to read up on these topics, I do actually have a basic understanding of these things, since I have tinkered with Intel Assembly and did write my own compiler in Python and then in Dart. Talking of mutability, i found it quite interesting that one uses mut in Rust. In Dart we use final which doesn't let us update the value of a variable, in a loop for instance. But reading your message is most kind and I will be sure to quiz around if I am unsure of something! :) Thank you!
    xiretza
    @xiretza:xiretza.xyz
    [m]
    @iamtheblackunicorn: I don't think gitter supports matrix spaces, but if you set up e.g. Element, you can join #rust-space:matrix.org for a collection of a bunch of other rust rooms. The most active general-purpose room right now is #rust:matrix.org, feel free to also join there if you have any questions that go unanswered here.
    jez:petrichor.me @jez:petrichor.me checks his gitter account to see if the matrix links work properly...
    Jez (he/him)
    @jez:petrichor.me
    [m]
    hmm, seems not
    but i think these versions will:
    if you've not got a matrix account, i think those links should give you the opportunity to sign up for one on matrix.org (other matrix homeservers are available! 😜)
    ✭ THE BLACK UNICORN ✭
    @iamtheblackunicorn
    @xiretza:xiretza.xyz Wow, thank you for informing about this, I will definitely investigate it further. I did always want to investigate matrix.org, just never found the right time! :)
    @jez:petrichor.me Thank you to you too! :)
    calebjasik
    @calebjasik:mozilla.org
    [m]
    hello from the aforementioned rust space 😆
    Giuseppe Cannella
    @gekomad
    hi, how can I add -funroll-loops ? on my compilation?
    Jez (he/him)
    @jez:petrichor.me
    [m]
    doesn't rust unroll loops automatically?
    tanriol
    @tanriol:matrix.org
    [m]
    AFAIK, it does in release mode.
    Giuseppe Cannella
    @gekomad
    ok thanks
    Kjartan
    @kjartan_oli:matrix.org
    [m]
    I'm writing a small CLI tool in Rust. In this CLI tool there is a struct whose new function returns a Result<struct, Box<dyn Error>, because it could fail both on being passed numbers which are out of range, and because the user could pass non-number arguments which would cause parse() to fail. Now I'm trying to write tests for this struct, and if possible I would like some of them to be able tell what kind of error is returned. Is it somehow possible to check the concrete type of a trait object, should I convert the integer parse errors to a variant of the error enum I have for this struct, or should I not be trying to do this?
    Jez (he/him)
    @jez:petrichor.me
    [m]
    I've not used them myself, but is this what the is and downcast methods on std::error::Error are for?
    Michal 'vorner' Vaner
    @vorner
    Also, you can have a crate that works with errors and might provide some additional conveniences ‒ anyhow (which has its own error type that wraps the errors) or err-context (that just adds methods to Box<dyn Error + Send + Sync>). As a side note, I would recommend you useBox<dyn Error + Send + Sync>` ‒ almost all errors are Send + Sync, but if you don't list them in the Box thing, it "fo
    1 reply
    rgets" they are Send/Sync and will make working with the boxed thing harder.
    Jez (he/him)
    @jez:petrichor.me
    [m]
    Oh yes, anyhow is nice!
    Anders Blenstrup-Pedersen
    @KatsuoRyuu
    Hi everyone, Im playing around trying to get a bit better using dynamic/templates, and i needed a tool where i can operate rails (map, map_err etc) on Futures aka async returns.
    U currently have the following, but i think i might have worked myself a bit into a corner here thought, so would like to know if anybody have any suggestions?
    #[async_trait]
    trait AsyncResult<T, E> {
        async fn map<U, F: FnOnce(T) -> U>(
            self,
            op: F,
        ) -> dyn Future<Output = Result<U, E>> + ?Sized + Send;
    }
    #[async_trait]
    impl<T, E, L> AsyncResult<T, E> for L
    where
        L: Sized + Future<Output = Result<T, E>> + Send,
    {
        async fn map<U, F: FnOnce(T) -> U>(self, op: F) -> Future<Output = Result<U, E>> {
            match self.await {
                Ok(t) => Ok(op(t)),
                Err(e) => Err(e),
            }
        }
    }
    magnus
    @magnusja
    Compiler talking in riddles to me ...
    error: implementation of `FnOnce` is not general enough
       --> epibator/src/db/ems_part_number/mod.rs:162:65
        |
    162 |       ) -> Result<Vec<DomainEmsPartNumberPartMatch>, Self::Error> {
        |  _________________________________________________________________^
    163 | |         // currently we only use MPN for match
    164 | |         let ipns = ipns
    165 | |             .into_iter()
    ...   |
    242 | |         Ok(part_matches)
    243 | |     }
        | |_____^ implementation of `FnOnce` is not general enough
        |
        = note: closure with signature `fn(&'0 domain::ems_part_number::EmsInternalPartNumber) -> Vec<std::string::String>` must implement `FnOnce<(&domain::ems_part_number::EmsInternalPartNumber,)>`, for any lifetime `'0`...
        = note: ...but it actually implements `FnOnce<(&domain::ems_part_number::EmsInternalPartNumber,)>`
    let ots_parts = self
                .find_by_mpn_exact_match(
                    ipns.iter()
                        .map(|ipn: &DomainEmsInternalPartNumber| -> Vec<String> {
                            ipn.part_specifications
                                .iter()
                                .map(|p| p.mpn.clone())
                                .flatten()
                                .collect()
                        })
                        .flatten()
                        .collect(),
                )
                .await?;
    WTF?!
    tanriol
    @tanriol:matrix.org
    [m]
    @magnusja: Is this the same part of the code?..
    Devin R
    @devinr528:matrix.org
    [m]
    Is there any chance it's similar to rust-lang/rust#87079 or rust-lang/rust#70263
    magnus
    @magnusja

    Yes the snipped I posted is definitely the part of the code causing this error. If I clone the vec instead like this

    let ots_parts = self
                .find_by_mpn_exact_match(
                    ipns.iter()
                        .cloned()
                        .map(|ipn| -> Vec<String> {
                            ipn.part_specifications
                                .into_iter()
                                .map(|p| p.mpn)
                                .flatten()
                                .collect()
                        })
                        .flatten()
                        .collect()
                )
                .await?;

    It is actually compiling

    (oh and I removed the type annotaions but that was definitely not the problem)
    * one type annotation
    @devinr528:matrix.org Yeah these issues look quite similar, unfortunately I cannot change the implementation of map