Created
September 27, 2012 18:12
-
-
Save alco/3795486 to your computer and use it in GitHub Desktop.
Rust vs Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
I'm sure this is not a complete working code. Some includes/imports must be missing. | |
*/ | |
fn main() { | |
for ["Alice", "Bob", "Carol"].each |name| { | |
let name = copy *name; | |
do task::spawn { | |
let v = rand::Rng().shuffle([1, 2, 3]); | |
for v.each |num| { | |
io::print(fmt!("%s says: '%d'\n", name, *num)) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a complete working program, equivalent to the Rust program. | |
*/ | |
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
func shuffle(nums []int) []int { | |
result := make([]int, len(nums)) | |
indices := rand.Perm(len(nums)) | |
for i, si := range indices { | |
result[i] = nums[si] | |
} | |
return result | |
} | |
func main() { | |
people := []string{"Alice", "Bob", "Carol"} | |
for _, name := range people { | |
go func(name string) { | |
v := shuffle([]int{1, 2, 3}) | |
for _, num := range v { | |
fmt.Printf("%s says: '%d'\n", name, num) | |
} | |
}(name) | |
} | |
time.Sleep(10 * time.Millisecond) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment