Why do owned slices take 16 bytes in rust? (on x64 machine) Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Why does Rust borrow checker reject this code?Is &[T] literally an alias of Slice in rust?Why doesn't println! work in Rust unit tests?What is vector slice in Rust?Why are Rust executables so huge?Why are explicit lifetimes needed in Rust?Why is a &str called a slice in Rust?How To Assign Slices While Iterating Over a Vec in Rust without copying?How can I get a single byte from stdin in Rust without waiting for Enter?Creating a slice and appending to it in Rust

Suing a Police Officer Instead of the Police Department

Has a Nobel Peace laureate ever been accused of war crimes?

Will I lose my paid in full property

Not within Jobscope - Aggravated injury

VBA: Single line if statement with multiple actions

Determinant of a matrix with 2 equal rows

Why did Israel vote against lifting the American embargo on Cuba?

Writing a T-SQL stored procedure to receive 4 numbers and insert them into a table

Calculating the expected value of truncated normal

Translate text contents of an existing file from lower to upper case and copy to a new file

What's called a person who work as someone who puts products on shelves in stores?

Why is water being consumed when my shutoff valve is closed?

How was Lagrange appointed professor of mathematics so early?

What is a 'Key' in computer science?

How do I deal with an erroneously large refund?

`FindRoot [ ]`::jsing: Encountered a singular Jacobian at a point...WHY

Israeli soda type drink

What is the ongoing value of the Kanban board to the developers as opposed to management

Why would the Overseers waste their stock of slaves on the Game?

Married in secret, can marital status in passport be changed at a later date?

Like totally amazing interchangeable sister outfit accessory swapping or whatever

Variable does not exist: sObjectType (Task.sObjectType)

A journey... into the MIND

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'



Why do owned slices take 16 bytes in rust? (on x64 machine)



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Why does Rust borrow checker reject this code?Is &[T] literally an alias of Slice in rust?Why doesn't println! work in Rust unit tests?What is vector slice in Rust?Why are Rust executables so huge?Why are explicit lifetimes needed in Rust?Why is a &str called a slice in Rust?How To Assign Slices While Iterating Over a Vec in Rust without copying?How can I get a single byte from stdin in Rust without waiting for Enter?Creating a slice and appending to it in Rust



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








10















fn main() 
// prints 8, 8, 16
println!(
", , ",
std::mem::size_of::<Box<i8>>(),
std::mem::size_of::<Box<&[i8]>>(),
std::mem::size_of::<Box<[i8]>>(),
);



Why do owned slices take 16 bytes but referenced slices take only 8?










share|improve this question
























  • Playground: play.rust-lang.org/…

    – French Boiethios
    6 hours ago






  • 2





    Previous discussion: i.stack.imgur.com/Xt6L3.png

    – hellow
    6 hours ago

















10















fn main() 
// prints 8, 8, 16
println!(
", , ",
std::mem::size_of::<Box<i8>>(),
std::mem::size_of::<Box<&[i8]>>(),
std::mem::size_of::<Box<[i8]>>(),
);



Why do owned slices take 16 bytes but referenced slices take only 8?










share|improve this question
























  • Playground: play.rust-lang.org/…

    – French Boiethios
    6 hours ago






  • 2





    Previous discussion: i.stack.imgur.com/Xt6L3.png

    – hellow
    6 hours ago













10












10








10








fn main() 
// prints 8, 8, 16
println!(
", , ",
std::mem::size_of::<Box<i8>>(),
std::mem::size_of::<Box<&[i8]>>(),
std::mem::size_of::<Box<[i8]>>(),
);



Why do owned slices take 16 bytes but referenced slices take only 8?










share|improve this question
















fn main() 
// prints 8, 8, 16
println!(
", , ",
std::mem::size_of::<Box<i8>>(),
std::mem::size_of::<Box<&[i8]>>(),
std::mem::size_of::<Box<[i8]>>(),
);



Why do owned slices take 16 bytes but referenced slices take only 8?







rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 6 hours ago









Boann

37.6k1291123




37.6k1291123










asked 7 hours ago









aminamin

1,28812042




1,28812042












  • Playground: play.rust-lang.org/…

    – French Boiethios
    6 hours ago






  • 2





    Previous discussion: i.stack.imgur.com/Xt6L3.png

    – hellow
    6 hours ago

















  • Playground: play.rust-lang.org/…

    – French Boiethios
    6 hours ago






  • 2





    Previous discussion: i.stack.imgur.com/Xt6L3.png

    – hellow
    6 hours ago
















Playground: play.rust-lang.org/…

– French Boiethios
6 hours ago





Playground: play.rust-lang.org/…

– French Boiethios
6 hours ago




2




2





Previous discussion: i.stack.imgur.com/Xt6L3.png

– hellow
6 hours ago





Previous discussion: i.stack.imgur.com/Xt6L3.png

– hellow
6 hours ago












2 Answers
2






active

oldest

votes


















12














Box<T> is basically *const T (Actually it's a newtype around Unique<T>, which itself is a NonNull<T> with PhantomData<T> (for dropck), but let's stick to *const T for simplicity).



A pointer in rust normally has the same size as size_of::<usize>() except when T is a Dynamically sized type (DST). Currently, a Box<DST> is 2 * size_of::<usize>() in size (The exact representation is not stable at the time of writing). A pointer to a DST is called FatPtr.



Currently, there are two kinds of DSTs: Slices and traits. A FatPtr to a slice is defined like this:



#[repr(C)]
struct FatPtr<T>
data: *const T,
len: usize,



For a trait pointer, len is replaced by a pointer to the vtable.




  • Box<i8>: i8 is a sized type => basically the same as *const i8 => 8 bytes in size (on 64 bit)


  • Box<[i8]>: [i8] is a DST => basically the same as FatPtr<T> => 16 bytes in size (on 64 bit)


  • Box<&[i8]>: &[i8] is not a DST. It's basically the same as *const FatPtr<T> => 8 bytes in size (on 64 bit)





share|improve this answer
































    1














    The size of a reference depends on the "sizeness" of the referenced type:



    • A reference to a sized type is a single pointer to the memory address.


    • A reference to an unsized type is a pointer to the memory and the size of the pointed datum. That's what is called a fat pointer:



      #[repr(C)]
      struct FatPtr<T>
      data: *const T,
      len: usize,



    A Box is a special kind of pointer that points to the heap, but it is a pointer anyway.



    Knowing that, you understand that:




    • Box<i8> is 8 bytes because i8 is sized,


    • Box<&[i8]> is 8 bytes because a reference is sized,


    • Box<[i8]> is 8 bytes because a slice is unsized





    share|improve this answer























    • Box<[i8]> is 16 in your own playground link

      – Stargateur
      7 mins ago











    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55814114%2fwhy-do-owned-slices-take-16-bytes-in-rust-on-x64-machine%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    Box<T> is basically *const T (Actually it's a newtype around Unique<T>, which itself is a NonNull<T> with PhantomData<T> (for dropck), but let's stick to *const T for simplicity).



    A pointer in rust normally has the same size as size_of::<usize>() except when T is a Dynamically sized type (DST). Currently, a Box<DST> is 2 * size_of::<usize>() in size (The exact representation is not stable at the time of writing). A pointer to a DST is called FatPtr.



    Currently, there are two kinds of DSTs: Slices and traits. A FatPtr to a slice is defined like this:



    #[repr(C)]
    struct FatPtr<T>
    data: *const T,
    len: usize,



    For a trait pointer, len is replaced by a pointer to the vtable.




    • Box<i8>: i8 is a sized type => basically the same as *const i8 => 8 bytes in size (on 64 bit)


    • Box<[i8]>: [i8] is a DST => basically the same as FatPtr<T> => 16 bytes in size (on 64 bit)


    • Box<&[i8]>: &[i8] is not a DST. It's basically the same as *const FatPtr<T> => 8 bytes in size (on 64 bit)





    share|improve this answer





























      12














      Box<T> is basically *const T (Actually it's a newtype around Unique<T>, which itself is a NonNull<T> with PhantomData<T> (for dropck), but let's stick to *const T for simplicity).



      A pointer in rust normally has the same size as size_of::<usize>() except when T is a Dynamically sized type (DST). Currently, a Box<DST> is 2 * size_of::<usize>() in size (The exact representation is not stable at the time of writing). A pointer to a DST is called FatPtr.



      Currently, there are two kinds of DSTs: Slices and traits. A FatPtr to a slice is defined like this:



      #[repr(C)]
      struct FatPtr<T>
      data: *const T,
      len: usize,



      For a trait pointer, len is replaced by a pointer to the vtable.




      • Box<i8>: i8 is a sized type => basically the same as *const i8 => 8 bytes in size (on 64 bit)


      • Box<[i8]>: [i8] is a DST => basically the same as FatPtr<T> => 16 bytes in size (on 64 bit)


      • Box<&[i8]>: &[i8] is not a DST. It's basically the same as *const FatPtr<T> => 8 bytes in size (on 64 bit)





      share|improve this answer



























        12












        12








        12







        Box<T> is basically *const T (Actually it's a newtype around Unique<T>, which itself is a NonNull<T> with PhantomData<T> (for dropck), but let's stick to *const T for simplicity).



        A pointer in rust normally has the same size as size_of::<usize>() except when T is a Dynamically sized type (DST). Currently, a Box<DST> is 2 * size_of::<usize>() in size (The exact representation is not stable at the time of writing). A pointer to a DST is called FatPtr.



        Currently, there are two kinds of DSTs: Slices and traits. A FatPtr to a slice is defined like this:



        #[repr(C)]
        struct FatPtr<T>
        data: *const T,
        len: usize,



        For a trait pointer, len is replaced by a pointer to the vtable.




        • Box<i8>: i8 is a sized type => basically the same as *const i8 => 8 bytes in size (on 64 bit)


        • Box<[i8]>: [i8] is a DST => basically the same as FatPtr<T> => 16 bytes in size (on 64 bit)


        • Box<&[i8]>: &[i8] is not a DST. It's basically the same as *const FatPtr<T> => 8 bytes in size (on 64 bit)





        share|improve this answer















        Box<T> is basically *const T (Actually it's a newtype around Unique<T>, which itself is a NonNull<T> with PhantomData<T> (for dropck), but let's stick to *const T for simplicity).



        A pointer in rust normally has the same size as size_of::<usize>() except when T is a Dynamically sized type (DST). Currently, a Box<DST> is 2 * size_of::<usize>() in size (The exact representation is not stable at the time of writing). A pointer to a DST is called FatPtr.



        Currently, there are two kinds of DSTs: Slices and traits. A FatPtr to a slice is defined like this:



        #[repr(C)]
        struct FatPtr<T>
        data: *const T,
        len: usize,



        For a trait pointer, len is replaced by a pointer to the vtable.




        • Box<i8>: i8 is a sized type => basically the same as *const i8 => 8 bytes in size (on 64 bit)


        • Box<[i8]>: [i8] is a DST => basically the same as FatPtr<T> => 16 bytes in size (on 64 bit)


        • Box<&[i8]>: &[i8] is not a DST. It's basically the same as *const FatPtr<T> => 8 bytes in size (on 64 bit)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 6 hours ago

























        answered 6 hours ago









        Tim DiekmannTim Diekmann

        3,56291940




        3,56291940























            1














            The size of a reference depends on the "sizeness" of the referenced type:



            • A reference to a sized type is a single pointer to the memory address.


            • A reference to an unsized type is a pointer to the memory and the size of the pointed datum. That's what is called a fat pointer:



              #[repr(C)]
              struct FatPtr<T>
              data: *const T,
              len: usize,



            A Box is a special kind of pointer that points to the heap, but it is a pointer anyway.



            Knowing that, you understand that:




            • Box<i8> is 8 bytes because i8 is sized,


            • Box<&[i8]> is 8 bytes because a reference is sized,


            • Box<[i8]> is 8 bytes because a slice is unsized





            share|improve this answer























            • Box<[i8]> is 16 in your own playground link

              – Stargateur
              7 mins ago















            1














            The size of a reference depends on the "sizeness" of the referenced type:



            • A reference to a sized type is a single pointer to the memory address.


            • A reference to an unsized type is a pointer to the memory and the size of the pointed datum. That's what is called a fat pointer:



              #[repr(C)]
              struct FatPtr<T>
              data: *const T,
              len: usize,



            A Box is a special kind of pointer that points to the heap, but it is a pointer anyway.



            Knowing that, you understand that:




            • Box<i8> is 8 bytes because i8 is sized,


            • Box<&[i8]> is 8 bytes because a reference is sized,


            • Box<[i8]> is 8 bytes because a slice is unsized





            share|improve this answer























            • Box<[i8]> is 16 in your own playground link

              – Stargateur
              7 mins ago













            1












            1








            1







            The size of a reference depends on the "sizeness" of the referenced type:



            • A reference to a sized type is a single pointer to the memory address.


            • A reference to an unsized type is a pointer to the memory and the size of the pointed datum. That's what is called a fat pointer:



              #[repr(C)]
              struct FatPtr<T>
              data: *const T,
              len: usize,



            A Box is a special kind of pointer that points to the heap, but it is a pointer anyway.



            Knowing that, you understand that:




            • Box<i8> is 8 bytes because i8 is sized,


            • Box<&[i8]> is 8 bytes because a reference is sized,


            • Box<[i8]> is 8 bytes because a slice is unsized





            share|improve this answer













            The size of a reference depends on the "sizeness" of the referenced type:



            • A reference to a sized type is a single pointer to the memory address.


            • A reference to an unsized type is a pointer to the memory and the size of the pointed datum. That's what is called a fat pointer:



              #[repr(C)]
              struct FatPtr<T>
              data: *const T,
              len: usize,



            A Box is a special kind of pointer that points to the heap, but it is a pointer anyway.



            Knowing that, you understand that:




            • Box<i8> is 8 bytes because i8 is sized,


            • Box<&[i8]> is 8 bytes because a reference is sized,


            • Box<[i8]> is 8 bytes because a slice is unsized






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 6 hours ago









            French BoiethiosFrench Boiethios

            11.4k44081




            11.4k44081












            • Box<[i8]> is 16 in your own playground link

              – Stargateur
              7 mins ago

















            • Box<[i8]> is 16 in your own playground link

              – Stargateur
              7 mins ago
















            Box<[i8]> is 16 in your own playground link

            – Stargateur
            7 mins ago





            Box<[i8]> is 16 in your own playground link

            – Stargateur
            7 mins ago

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55814114%2fwhy-do-owned-slices-take-16-bytes-in-rust-on-x64-machine%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Oświęcim Innehåll Historia | Källor | Externa länkar | Navigeringsmeny50°2′18″N 19°13′17″Ö / 50.03833°N 19.22139°Ö / 50.03833; 19.2213950°2′18″N 19°13′17″Ö / 50.03833°N 19.22139°Ö / 50.03833; 19.221393089658Nordisk familjebok, AuschwitzInsidan tro och existensJewish Community i OświęcimAuschwitz Jewish Center: MuseumAuschwitz Jewish Center

            Valle di Casies Indice Geografia fisica | Origini del nome | Storia | Società | Amministrazione | Sport | Note | Bibliografia | Voci correlate | Altri progetti | Collegamenti esterni | Menu di navigazione46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)Sito istituzionaleAstat Censimento della popolazione 2011 - Determinazione della consistenza dei tre gruppi linguistici della Provincia Autonoma di Bolzano-Alto Adige - giugno 2012Numeri e fattiValle di CasiesDato IstatTabella dei gradi/giorno dei Comuni italiani raggruppati per Regione e Provincia26 agosto 1993, n. 412Heraldry of the World: GsiesStatistiche I.StatValCasies.comWikimedia CommonsWikimedia CommonsValle di CasiesSito ufficialeValle di CasiesMM14870458910042978-6

            Typsetting diagram chases (with TikZ?) Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)How to define the default vertical distance between nodes?Draw edge on arcNumerical conditional within tikz keys?TikZ: Drawing an arc from an intersection to an intersectionDrawing rectilinear curves in Tikz, aka an Etch-a-Sketch drawingLine up nested tikz enviroments or how to get rid of themHow to place nodes in an absolute coordinate system in tikzCommutative diagram with curve connecting between nodesTikz with standalone: pinning tikz coordinates to page cmDrawing a Decision Diagram with Tikz and layout manager