Spacc BBS Spacc BBS
    • Categorie
    • Recenti
    • Tag
    • Popolare
    • Mondo
    • Utenti
    • Gruppi
    • Registrati
    • Accedi
    La nuova BBS è in fase Alpha. I post precedenti al 22 luglio 2024 potrebbero non essere trasferibili, ma rimarranno disponibili per la lettura su /old/.

    Just remembered you can also do vec![value; length], and not just vec![value1, value2, value3...] to create a Vec.

    Pianificato Fissato Bloccato Spostato Uncategorized
    rust
    12 Post 2 Autori 2 Visualizzazioni
    Caricamento altri post
    • Da Vecchi a Nuovi
    • Da Nuovi a Vecchi
    • Più Voti
    Rispondi
    • Topic risposta
    Effettua l'accesso per rispondere
    Questa discussione è stata eliminata. Solo gli utenti con diritti di gestione possono vederla.
    • steffo@a.junimo.partyS Questo utente è esterno a questo forum
      steffo@a.junimo.party
      ultima modifica di

      Just remembered you can also do vec![value; length], and not just vec![value1, value2, value3...] to create a Vec.

      Link Preview Image
      vec in std - Rust

      Creates a `Vec` containing the arguments.

      favicon

      (doc.rust-lang.org)

      #Rust

      steffo@a.junimo.partyS 1 Risposta Ultima Risposta Rispondi Cita 0
      • steffo@a.junimo.partyS Questo utente è esterno a questo forum
        steffo@a.junimo.party @steffo@a.junimo.party
        ultima modifica di

        Cool thing is that it repeatedly uses Clone on the given value instead of Copy, so you can give it pretty much everything as input!

        #Rust

        wolvericcatkin@tech.lgbtW 1 Risposta Ultima Risposta Rispondi Cita 0
        • wolvericcatkin@tech.lgbtW Questo utente è esterno a questo forum
          wolvericcatkin@tech.lgbt @steffo@a.junimo.party
          ultima modifica di

          @steffo Also of note: std::iter::repeat_n, and Iterator::take and Iterator::collect...

          steffo@a.junimo.partyS 1 Risposta Ultima Risposta Rispondi Cita 0
          • steffo@a.junimo.partyS Questo utente è esterno a questo forum
            steffo@a.junimo.party @wolvericcatkin@tech.lgbt
            ultima modifica di

            @WolvericCatkin oh yeah i knew about those, i had just completely forgotten about the vec! macro lol

            wolvericcatkin@tech.lgbtW 1 Risposta Ultima Risposta Rispondi Cita 0
            • wolvericcatkin@tech.lgbtW Questo utente è esterno a questo forum
              wolvericcatkin@tech.lgbt @steffo@a.junimo.party
              ultima modifica di

              @steffo Tbh, reading the methods vec! ends up expanding out to, they seem like a bit of a mess... repeat_n may be the better option from a technical standpoint...

              steffo@a.junimo.partyS 1 Risposta Ultima Risposta Rispondi Cita 0
              • steffo@a.junimo.partyS Questo utente è esterno a questo forum
                steffo@a.junimo.party @wolvericcatkin@tech.lgbt
                ultima modifica di

                @WolvericCatkin

                don't you lose information about capacity that way, though?

                iirc iterators can only provide a size_hint, not give guarantees about their size

                does the implementation of collect<Vec<_>> take them into account?

                wolvericcatkin@tech.lgbtW 1 Risposta Ultima Risposta Rispondi Cita 0
                • wolvericcatkin@tech.lgbtW Questo utente è esterno a questo forum
                  wolvericcatkin@tech.lgbt @steffo@a.junimo.party
                  ultima modifica di

                  @steffo std::iter::repeat_n has a guaranteed size, because it has a set number of iterators, and is cloning the item until the last iteration, so it's also ExactLenIterator.

                  Depending on the provider, Vec::from_iterator can take advantage of Iterator::size_hint.

                  wolvericcatkin@tech.lgbtW steffo@a.junimo.partyS 2 Risposte Ultima Risposta Rispondi Cita 0
                  • wolvericcatkin@tech.lgbtW Questo utente è esterno a questo forum
                    wolvericcatkin@tech.lgbt @wolvericcatkin@tech.lgbt
                    ultima modifica di

                    @steffo This file:
                    https://doc.rust-lang.org/src/alloc/vec/spec_from_iter_nested.rs.html

                    wolvericcatkin@tech.lgbtW 1 Risposta Ultima Risposta Rispondi Cita 0
                    • steffo@a.junimo.partyS Questo utente è esterno a questo forum
                      steffo@a.junimo.party @wolvericcatkin@tech.lgbt
                      ultima modifica di

                      @WolvericCatkin oh that's very nice then!

                      i'll admit i haven't looked very much into the internals of Iterator and have been treating them pretty much as opaque types

                      1 Risposta Ultima Risposta Rispondi Cita 0
                      • wolvericcatkin@tech.lgbtW Questo utente è esterno a questo forum
                        wolvericcatkin@tech.lgbt @wolvericcatkin@tech.lgbt
                        ultima modifica di

                        @steffo vec! ends up expanding out into a function that uses Vec::extend_with, which explicitly notes it has to compensate for compiler bugs, but otherwise has quite similar implementation...

                        wolvericcatkin@tech.lgbtW 1 Risposta Ultima Risposta Rispondi Cita 0
                        • wolvericcatkin@tech.lgbtW Questo utente è esterno a questo forum
                          wolvericcatkin@tech.lgbt @wolvericcatkin@tech.lgbt
                          ultima modifica di

                          @steffo Bruh- ok, apparently looking at where Vec::from_iterator ends up, it does end up running into the same bug as vec! eventually, just in a different method... Iterator handling with Vec is so frustratingly indirected...

                          It does still pre-allocate with Iterator::size_hint though.

                          steffo@a.junimo.partyS 1 Risposta Ultima Risposta Rispondi Cita 0
                          • steffo@a.junimo.partyS Questo utente è esterno a questo forum
                            steffo@a.junimo.party @wolvericcatkin@tech.lgbt
                            ultima modifica di

                            @WolvericCatkin hahahaha, seems like there's no way around it

                            1 Risposta Ultima Risposta Rispondi Cita 0
                            • Primo post
                              Ultimo post