Does manually breaking early from a for loop make sense when testing if a linked list contains an item?










3














While following a basic tutorial, I'm faced with this function:



use std::collections::LinkedList;

// ...

pub fn contains(&self, x: i32, y: i32) -> bool
let mut ch = 0;
let list: &LinkedList<Block> = &self.body;

for block in list
if block.x == x && block.y == y
return true;

ch += 1;
if ch == list.len() - 1
break;



return false;



It felt obvious that I could get rid of the whole if ch == list.len() - 1 part and write it like so:



pub fn contains(&self, x: i32, y: i32) -> bool 
for block in &self.body
if block.x == x && block.y == y
return true;


return false;



It seems to work fine, but maybe there is something that I've missed? Is it just an unnecessary overhead that an author of the tutorial made by mistake?










share|improve this question



















  • 1




    Is this the LinkedList from the standard library? If not, perhaps it is a circularly linked list that iterates infinitely by linking the tail to the head.
    – Benjamin Lindley
    Nov 12 '18 at 23:12










  • Yes, it is. use std::collections::LinkedList;
    – streletss
    Nov 12 '18 at 23:14






  • 4




    even better self.body.iter().any(|block| block.x == x && block.y == y)
    – Stargateur
    Nov 13 '18 at 0:51






  • 1




    The presented code has numerous non-idiomatic aspects, so I would not trust that tutorial strongly.
    – Shepmaster
    Nov 13 '18 at 2:50















3














While following a basic tutorial, I'm faced with this function:



use std::collections::LinkedList;

// ...

pub fn contains(&self, x: i32, y: i32) -> bool
let mut ch = 0;
let list: &LinkedList<Block> = &self.body;

for block in list
if block.x == x && block.y == y
return true;

ch += 1;
if ch == list.len() - 1
break;



return false;



It felt obvious that I could get rid of the whole if ch == list.len() - 1 part and write it like so:



pub fn contains(&self, x: i32, y: i32) -> bool 
for block in &self.body
if block.x == x && block.y == y
return true;


return false;



It seems to work fine, but maybe there is something that I've missed? Is it just an unnecessary overhead that an author of the tutorial made by mistake?










share|improve this question



















  • 1




    Is this the LinkedList from the standard library? If not, perhaps it is a circularly linked list that iterates infinitely by linking the tail to the head.
    – Benjamin Lindley
    Nov 12 '18 at 23:12










  • Yes, it is. use std::collections::LinkedList;
    – streletss
    Nov 12 '18 at 23:14






  • 4




    even better self.body.iter().any(|block| block.x == x && block.y == y)
    – Stargateur
    Nov 13 '18 at 0:51






  • 1




    The presented code has numerous non-idiomatic aspects, so I would not trust that tutorial strongly.
    – Shepmaster
    Nov 13 '18 at 2:50













3












3








3


0





While following a basic tutorial, I'm faced with this function:



use std::collections::LinkedList;

// ...

pub fn contains(&self, x: i32, y: i32) -> bool
let mut ch = 0;
let list: &LinkedList<Block> = &self.body;

for block in list
if block.x == x && block.y == y
return true;

ch += 1;
if ch == list.len() - 1
break;



return false;



It felt obvious that I could get rid of the whole if ch == list.len() - 1 part and write it like so:



pub fn contains(&self, x: i32, y: i32) -> bool 
for block in &self.body
if block.x == x && block.y == y
return true;


return false;



It seems to work fine, but maybe there is something that I've missed? Is it just an unnecessary overhead that an author of the tutorial made by mistake?










share|improve this question















While following a basic tutorial, I'm faced with this function:



use std::collections::LinkedList;

// ...

pub fn contains(&self, x: i32, y: i32) -> bool
let mut ch = 0;
let list: &LinkedList<Block> = &self.body;

for block in list
if block.x == x && block.y == y
return true;

ch += 1;
if ch == list.len() - 1
break;



return false;



It felt obvious that I could get rid of the whole if ch == list.len() - 1 part and write it like so:



pub fn contains(&self, x: i32, y: i32) -> bool 
for block in &self.body
if block.x == x && block.y == y
return true;


return false;



It seems to work fine, but maybe there is something that I've missed? Is it just an unnecessary overhead that an author of the tutorial made by mistake?







rust for-in-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 2:50









Shepmaster

149k12287422




149k12287422










asked Nov 12 '18 at 22:35









streletssstreletss

2,080421




2,080421







  • 1




    Is this the LinkedList from the standard library? If not, perhaps it is a circularly linked list that iterates infinitely by linking the tail to the head.
    – Benjamin Lindley
    Nov 12 '18 at 23:12










  • Yes, it is. use std::collections::LinkedList;
    – streletss
    Nov 12 '18 at 23:14






  • 4




    even better self.body.iter().any(|block| block.x == x && block.y == y)
    – Stargateur
    Nov 13 '18 at 0:51






  • 1




    The presented code has numerous non-idiomatic aspects, so I would not trust that tutorial strongly.
    – Shepmaster
    Nov 13 '18 at 2:50












  • 1




    Is this the LinkedList from the standard library? If not, perhaps it is a circularly linked list that iterates infinitely by linking the tail to the head.
    – Benjamin Lindley
    Nov 12 '18 at 23:12










  • Yes, it is. use std::collections::LinkedList;
    – streletss
    Nov 12 '18 at 23:14






  • 4




    even better self.body.iter().any(|block| block.x == x && block.y == y)
    – Stargateur
    Nov 13 '18 at 0:51






  • 1




    The presented code has numerous non-idiomatic aspects, so I would not trust that tutorial strongly.
    – Shepmaster
    Nov 13 '18 at 2:50







1




1




Is this the LinkedList from the standard library? If not, perhaps it is a circularly linked list that iterates infinitely by linking the tail to the head.
– Benjamin Lindley
Nov 12 '18 at 23:12




Is this the LinkedList from the standard library? If not, perhaps it is a circularly linked list that iterates infinitely by linking the tail to the head.
– Benjamin Lindley
Nov 12 '18 at 23:12












Yes, it is. use std::collections::LinkedList;
– streletss
Nov 12 '18 at 23:14




Yes, it is. use std::collections::LinkedList;
– streletss
Nov 12 '18 at 23:14




4




4




even better self.body.iter().any(|block| block.x == x && block.y == y)
– Stargateur
Nov 13 '18 at 0:51




even better self.body.iter().any(|block| block.x == x && block.y == y)
– Stargateur
Nov 13 '18 at 0:51




1




1




The presented code has numerous non-idiomatic aspects, so I would not trust that tutorial strongly.
– Shepmaster
Nov 13 '18 at 2:50




The presented code has numerous non-idiomatic aspects, so I would not trust that tutorial strongly.
– Shepmaster
Nov 13 '18 at 2:50












1 Answer
1






active

oldest

votes


















2














As written in the original 'tutorial' version, it doesn't seem to look at the last element. Consider a list of length 2 where the second element is the one you're looking for.



After the first comparison, ch becomes 1. It's now equal to the list length minus 1, so you break out of the loop just before the loop would (if executed one more time) find the last element.



That doesn't make much sense, so I conclude yours is not only shorter but correct.






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%2f53271083%2fdoes-manually-breaking-early-from-a-for-loop-make-sense-when-testing-if-a-linked%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









    2














    As written in the original 'tutorial' version, it doesn't seem to look at the last element. Consider a list of length 2 where the second element is the one you're looking for.



    After the first comparison, ch becomes 1. It's now equal to the list length minus 1, so you break out of the loop just before the loop would (if executed one more time) find the last element.



    That doesn't make much sense, so I conclude yours is not only shorter but correct.






    share|improve this answer

























      2














      As written in the original 'tutorial' version, it doesn't seem to look at the last element. Consider a list of length 2 where the second element is the one you're looking for.



      After the first comparison, ch becomes 1. It's now equal to the list length minus 1, so you break out of the loop just before the loop would (if executed one more time) find the last element.



      That doesn't make much sense, so I conclude yours is not only shorter but correct.






      share|improve this answer























        2












        2








        2






        As written in the original 'tutorial' version, it doesn't seem to look at the last element. Consider a list of length 2 where the second element is the one you're looking for.



        After the first comparison, ch becomes 1. It's now equal to the list length minus 1, so you break out of the loop just before the loop would (if executed one more time) find the last element.



        That doesn't make much sense, so I conclude yours is not only shorter but correct.






        share|improve this answer












        As written in the original 'tutorial' version, it doesn't seem to look at the last element. Consider a list of length 2 where the second element is the one you're looking for.



        After the first comparison, ch becomes 1. It's now equal to the list length minus 1, so you break out of the loop just before the loop would (if executed one more time) find the last element.



        That doesn't make much sense, so I conclude yours is not only shorter but correct.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 0:55









        davedave

        1032




        1032



























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53271083%2fdoes-manually-breaking-early-from-a-for-loop-make-sense-when-testing-if-a-linked%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto