Understanding Rust function parameter type declarationHow do I print the type of a variable in Rust?Is it possible to declare the type of the variable in Rust for loops?Why doesn't println! work in Rust unit tests?Convert a String to int in Rust?Why are explicit lifetimes needed in Rust?In Rust how do you pass a function as a parameter?How to declare typed bitflags in Rust?How to understand this Rust function that returns another function?How does the dropping of temporary values work in rust?Cast Rust function declaration to the extern “C” declaration in Rust

Time travel short story where a man arrives in the late 19th century in a time machine and then sends the machine back into the past

Trouble understanding overseas colleagues

Ways to speed up user implemented RK4

Mapping a list into a phase plot

apt-get update is failing in debian

Applicability of Single Responsibility Principle

Is the destination of a commercial flight important for the pilot?

Bash method for viewing beginning and end of file

voltage of sounds of mp3files

What defines a dissertation?

Everything Bob says is false. How does he get people to trust him?

What is the intuitive meaning of having a linear relationship between the logs of two variables?

How does it work when somebody invests in my business?

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

What's the purpose of "true" in bash "if sudo true; then"

Cynical novel that describes an America ruled by the media, arms manufacturers, and ethnic figureheads

How to be diplomatic in refusing to write code that breaches the privacy of our users

Greatest common substring

Modify casing of marked letters

How does a character multiclassing into warlock get a focus?

What are the ramifications of creating a homebrew world without an Astral Plane?

Are there any comparative studies done between Ashtavakra Gita and Buddhim?

What is the oldest known work of fiction?

Why does John Bercow say “unlock” after reading out the results of a vote?



Understanding Rust function parameter type declaration


How do I print the type of a variable in Rust?Is it possible to declare the type of the variable in Rust for loops?Why doesn't println! work in Rust unit tests?Convert a String to int in Rust?Why are explicit lifetimes needed in Rust?In Rust how do you pass a function as a parameter?How to declare typed bitflags in Rust?How to understand this Rust function that returns another function?How does the dropping of temporary values work in rust?Cast Rust function declaration to the extern “C” declaration in Rust













1















I was reading the chapter on higher order functions of Rust by Example. Where they present the following canoncial example:



fn is_odd(n: u32) -> bool 
n % 2 == 1


fn main() acc, n_squared


Simple enough. But I realized that I don't understand the type of parameter n_squared. Both take_while and filter accept a function that takes a parameter by reference. That makes sense to me, you want to borrow instead of consuming the values in the map.



However, if n_squared is a reference, why don't I have to dereference it before comparing its value to limit or equaly surprising; why can I pass it directly to is_odd() without dereferencing?



I.e. why isn't it?



 |&n_squared| *n_squared < upper


When I try that the compiler gives the following error:



error[E0614]: type `integer` cannot be dereferenced
--> srchigherorder.rs:13:34
|
13 | .take_while(|&n_squared| *n_squared <= upper)
|


Indicating that n_squared is an i32 and not &i32. Looks like some sort pattern matching/destructuring is happening here, but I was unable to find the relevant documentation.










share|improve this question
























  • If you write |n_squared|, you'll get the expected error and using *n_squared in the body will fix it. See reddit.com/r/rust/comments/16b9wh/…

    – Alexey Romanov
    Mar 8 at 9:45







  • 1





    Since I edited the comment and you might not have seen it: I've added a link to an old Reddit discussion of exactly this issue reddit.com/r/rust/comments/16b9wh/…. While Rust has changed a lot, this seems to still be what's going happening.

    – Alexey Romanov
    Mar 8 at 9:53






  • 2





    The documentation is in doc.rust-lang.org/1.30.0/book/2018-edition/….`

    – Alexey Romanov
    Mar 8 at 11:35











  • @AlexeyRomanov Perfect! That's what I was looking for.

    – djf
    Mar 8 at 11:48















1















I was reading the chapter on higher order functions of Rust by Example. Where they present the following canoncial example:



fn is_odd(n: u32) -> bool 
n % 2 == 1


fn main() acc, n_squared


Simple enough. But I realized that I don't understand the type of parameter n_squared. Both take_while and filter accept a function that takes a parameter by reference. That makes sense to me, you want to borrow instead of consuming the values in the map.



However, if n_squared is a reference, why don't I have to dereference it before comparing its value to limit or equaly surprising; why can I pass it directly to is_odd() without dereferencing?



I.e. why isn't it?



 |&n_squared| *n_squared < upper


When I try that the compiler gives the following error:



error[E0614]: type `integer` cannot be dereferenced
--> srchigherorder.rs:13:34
|
13 | .take_while(|&n_squared| *n_squared <= upper)
|


Indicating that n_squared is an i32 and not &i32. Looks like some sort pattern matching/destructuring is happening here, but I was unable to find the relevant documentation.










share|improve this question
























  • If you write |n_squared|, you'll get the expected error and using *n_squared in the body will fix it. See reddit.com/r/rust/comments/16b9wh/…

    – Alexey Romanov
    Mar 8 at 9:45







  • 1





    Since I edited the comment and you might not have seen it: I've added a link to an old Reddit discussion of exactly this issue reddit.com/r/rust/comments/16b9wh/…. While Rust has changed a lot, this seems to still be what's going happening.

    – Alexey Romanov
    Mar 8 at 9:53






  • 2





    The documentation is in doc.rust-lang.org/1.30.0/book/2018-edition/….`

    – Alexey Romanov
    Mar 8 at 11:35











  • @AlexeyRomanov Perfect! That's what I was looking for.

    – djf
    Mar 8 at 11:48













1












1








1


0






I was reading the chapter on higher order functions of Rust by Example. Where they present the following canoncial example:



fn is_odd(n: u32) -> bool 
n % 2 == 1


fn main() acc, n_squared


Simple enough. But I realized that I don't understand the type of parameter n_squared. Both take_while and filter accept a function that takes a parameter by reference. That makes sense to me, you want to borrow instead of consuming the values in the map.



However, if n_squared is a reference, why don't I have to dereference it before comparing its value to limit or equaly surprising; why can I pass it directly to is_odd() without dereferencing?



I.e. why isn't it?



 |&n_squared| *n_squared < upper


When I try that the compiler gives the following error:



error[E0614]: type `integer` cannot be dereferenced
--> srchigherorder.rs:13:34
|
13 | .take_while(|&n_squared| *n_squared <= upper)
|


Indicating that n_squared is an i32 and not &i32. Looks like some sort pattern matching/destructuring is happening here, but I was unable to find the relevant documentation.










share|improve this question
















I was reading the chapter on higher order functions of Rust by Example. Where they present the following canoncial example:



fn is_odd(n: u32) -> bool 
n % 2 == 1


fn main() acc, n_squared


Simple enough. But I realized that I don't understand the type of parameter n_squared. Both take_while and filter accept a function that takes a parameter by reference. That makes sense to me, you want to borrow instead of consuming the values in the map.



However, if n_squared is a reference, why don't I have to dereference it before comparing its value to limit or equaly surprising; why can I pass it directly to is_odd() without dereferencing?



I.e. why isn't it?



 |&n_squared| *n_squared < upper


When I try that the compiler gives the following error:



error[E0614]: type `integer` cannot be dereferenced
--> srchigherorder.rs:13:34
|
13 | .take_while(|&n_squared| *n_squared <= upper)
|


Indicating that n_squared is an i32 and not &i32. Looks like some sort pattern matching/destructuring is happening here, but I was unable to find the relevant documentation.







rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 9:38







djf

















asked Mar 8 at 9:33









djfdjf

5,33463348




5,33463348












  • If you write |n_squared|, you'll get the expected error and using *n_squared in the body will fix it. See reddit.com/r/rust/comments/16b9wh/…

    – Alexey Romanov
    Mar 8 at 9:45







  • 1





    Since I edited the comment and you might not have seen it: I've added a link to an old Reddit discussion of exactly this issue reddit.com/r/rust/comments/16b9wh/…. While Rust has changed a lot, this seems to still be what's going happening.

    – Alexey Romanov
    Mar 8 at 9:53






  • 2





    The documentation is in doc.rust-lang.org/1.30.0/book/2018-edition/….`

    – Alexey Romanov
    Mar 8 at 11:35











  • @AlexeyRomanov Perfect! That's what I was looking for.

    – djf
    Mar 8 at 11:48

















  • If you write |n_squared|, you'll get the expected error and using *n_squared in the body will fix it. See reddit.com/r/rust/comments/16b9wh/…

    – Alexey Romanov
    Mar 8 at 9:45







  • 1





    Since I edited the comment and you might not have seen it: I've added a link to an old Reddit discussion of exactly this issue reddit.com/r/rust/comments/16b9wh/…. While Rust has changed a lot, this seems to still be what's going happening.

    – Alexey Romanov
    Mar 8 at 9:53






  • 2





    The documentation is in doc.rust-lang.org/1.30.0/book/2018-edition/….`

    – Alexey Romanov
    Mar 8 at 11:35











  • @AlexeyRomanov Perfect! That's what I was looking for.

    – djf
    Mar 8 at 11:48
















If you write |n_squared|, you'll get the expected error and using *n_squared in the body will fix it. See reddit.com/r/rust/comments/16b9wh/…

– Alexey Romanov
Mar 8 at 9:45






If you write |n_squared|, you'll get the expected error and using *n_squared in the body will fix it. See reddit.com/r/rust/comments/16b9wh/…

– Alexey Romanov
Mar 8 at 9:45





1




1





Since I edited the comment and you might not have seen it: I've added a link to an old Reddit discussion of exactly this issue reddit.com/r/rust/comments/16b9wh/…. While Rust has changed a lot, this seems to still be what's going happening.

– Alexey Romanov
Mar 8 at 9:53





Since I edited the comment and you might not have seen it: I've added a link to an old Reddit discussion of exactly this issue reddit.com/r/rust/comments/16b9wh/…. While Rust has changed a lot, this seems to still be what's going happening.

– Alexey Romanov
Mar 8 at 9:53




2




2





The documentation is in doc.rust-lang.org/1.30.0/book/2018-edition/….`

– Alexey Romanov
Mar 8 at 11:35





The documentation is in doc.rust-lang.org/1.30.0/book/2018-edition/….`

– Alexey Romanov
Mar 8 at 11:35













@AlexeyRomanov Perfect! That's what I was looking for.

– djf
Mar 8 at 11:48





@AlexeyRomanov Perfect! That's what I was looking for.

– djf
Mar 8 at 11:48












1 Answer
1






active

oldest

votes


















5














You are using function parameter destructuring:



|&n_squared| n_squared < upper


is functionally equivalent to:



|n_squared| *n_squared < upper


To understand this better, imagine you're passing a tuple of type &(i32, i32) to a lambda:



|&(x, y) : &(i32, i32)| x + y





share|improve this answer
























    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%2f55060328%2funderstanding-rust-function-parameter-type-declaration%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    You are using function parameter destructuring:



    |&n_squared| n_squared < upper


    is functionally equivalent to:



    |n_squared| *n_squared < upper


    To understand this better, imagine you're passing a tuple of type &(i32, i32) to a lambda:



    |&(x, y) : &(i32, i32)| x + y





    share|improve this answer





























      5














      You are using function parameter destructuring:



      |&n_squared| n_squared < upper


      is functionally equivalent to:



      |n_squared| *n_squared < upper


      To understand this better, imagine you're passing a tuple of type &(i32, i32) to a lambda:



      |&(x, y) : &(i32, i32)| x + y





      share|improve this answer



























        5












        5








        5







        You are using function parameter destructuring:



        |&n_squared| n_squared < upper


        is functionally equivalent to:



        |n_squared| *n_squared < upper


        To understand this better, imagine you're passing a tuple of type &(i32, i32) to a lambda:



        |&(x, y) : &(i32, i32)| x + y





        share|improve this answer















        You are using function parameter destructuring:



        |&n_squared| n_squared < upper


        is functionally equivalent to:



        |n_squared| *n_squared < upper


        To understand this better, imagine you're passing a tuple of type &(i32, i32) to a lambda:



        |&(x, y) : &(i32, i32)| x + y






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 at 11:50









        Svetlin Zarev

        5,93332760




        5,93332760










        answered Mar 8 at 10:12









        SirDariusSirDarius

        30.6k66284




        30.6k66284





























            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%2f55060328%2funderstanding-rust-function-parameter-type-declaration%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

            How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229