Re:Announcing Rust 1.19
by ofek on Thursday July 20, 2017 @02:10PM
(#14814945)
Wow, a break yielding a value from within a loop is awesome! Do any other langs have that?
[ Parent
| on HN ]
Sure, Ruby:
x = (1..100).each do |i|
if i > 9
break i
end
end
Evaluating that gives > x
=> 10
You can also supply a value to next within a loop, which is occasionally useful
Re:Announcing Rust 1.19
by joshmarlow on Thursday July 20, 2017 @02:23PM
(#14815053)
If you're using the loop macro, you can do it from Common Lisp (see [0]).
(block outer
(loop for i from 0 return 100) ; 100 returned from LOOP
(print "This will print")
200) ==> 200
(block outer
(loop for i from 0 do (return-from outer 100)) ; 100 returned from BLOCK
(print "This won't print")
200) ==> 100
[0] - http://www.gigamonkeys.com/book/loop-for-black-belts.htmlEDIT: I always do formatting wrong on here.
One can also break out of the LOOP and return the so far accumulated value:
CL-USER> (loop for i from 1 below 10
sum i
when (= i 6)
do (loop-finish))
21Not just with LOOP, RETURN works with every standard iteration construct.
I feel like this has to have precedent from somewhere, most likely from another expression-oriented language, but I can't seem to find one right now. I've checked the RFC expecting to see a discussion of precedence from other languages, but no luck so far.
Re:Announcing Rust 1.19
by matthewrudy on Thursday July 20, 2017 @03:28PM
(#14815562)
ruby does this
have code around my app doing things like:
loop do
code = random_string(6)
break code unless code_already_used?(code)
end