21. Swap values
交换变量a和b的值
1 | a, b = b, a |
1 | package main |
1 | 10 |
1 | fn main() { |
输出
a: 10, b: 3
or
1 | fn main() { |
输出
1 | a = 12, b = 42 |
22. Convert string to integer
将字符串转换为整型
1 | import "strconv" |
1 | package main |
1 | 123 |
or
1 | import "strconv" |
1 | package main |
1 | s is string 123 |
1 | fn main() { |
输出
1 | 123 |
or
1 | fn main() { |
输出
1 | 123 |
or
1 | fn main() { |
输出
1 | 123 |
23. Convert real number to string with 2 decimal places
Given a real number x, create its string representation s with 2 decimal digits following the dot.
给定一个实数,小数点后保留两位小数
1 | package main |
输出
3.14
1 | fn main() { |
输出
42.13
24. Assign to string the japanese word ネコ
Declare a new string s and initialize it with the literal value “ネコ” (which means “cat” in japanese)
声明一个新的字符串s,并用文字值“ネコ”初始化它(在日语中是“cat”的意思)
1 | package main |
1 | fn main() { |
25. Send a value to another thread
Share the string value “Alan” with an existing running process which will then display “Hello, Alan”
将字符串值“Alan”与现有的正在运行的进程共享,该进程将显示“你好,Alan”
1 | ch <- "Alan" |
1 | package main |
Hello, Alan
The receiver goroutine blocks reading the string channel ch.
The current goroutine sends the value to ch.
A goroutine is like a thread, but more lightweight.
1 | use std::thread; |
输出 Hello, "Alan"
26. Create a 2-dimensional array
Declare and initialize a matrix x having m rows and n columns, containing real numbers.
创建一个二维数组
声明并初始化一个有m行n列的矩阵x,包含实数。
1 | const m, n = 3, 4 |
1 | package main |
[[0 0 0 0] [0 0 8 0] [0 0 0 0]]
or
1 | package main |
[[0 0 0] [0 8 0]]
1 | fn main() { |
输出
1 | [ |
1 | fn main() { |
输出
1 | [ |
27. Create a 3-dimensional array
Declare and initialize a 3D array x, having dimensions boundaries m, n, p, and containing real numbers.
创建一个三维数组
声明并初始化一个三维数组x,它有m,n,p维边界,并且包含实数。
1 | const m, n, p = 2, 2, 3 |
1 | package main |
1 | [[[0 0 0] [0 0 0]] [[0 0 9] [0 0 0]]] |
or
1 | func make3D(m, n, p int) [][][]float64 { |
1 | package main |
1 | [[[0 0 0] [0 0 0]] [[0 0 9] [0 0 0]]] |
1 | fn main() { |
输出
1 |
|
1 | fn main() { |
输出
1 | [ |
28. Sort by a property
Sort elements of array-like collection items in ascending order of x.p, where p is a field of the type Item of the objects in items.
按x->p的升序对类似数组的集合项的元素进行排序,其中p是项中对象的项类型的字段。
1 | package main |
1 | Unsorted: [{twelve 12 english} {six 6 english} {eleven 11 english} {zero 0 english} {two 2 english}] |
or
1 | package main |
1 | Unsorted: [{twelve 12 english} {six 6 english} {eleven 11 english} {zero 0 english} {two 2 english}] |
1 |
|
输出
1 | [Foo { p: 1 }, Foo { p: 2 }, Foo { p: 3 }, Foo { p: 4 }] |
or
1 |
|
输出
1 | [Foo { p: 1 }, Foo { p: 2 }, Foo { p: 3 }, Foo { p: 4 }] |
29. Remove item from list, by its index
Remove i-th item from list items.
This will alter the original list or return a new list, depending on which is more idiomatic.
Note that in most languages, the smallest valid value for i is 0.
从列表项中删除第I项。
这将改变原来的列表或返回一个新的列表,这取决于哪个更习惯。
请注意,在大多数语言中,I的最小有效值是0。
1 | package main |
1 | [a b c d e f] |
or
1 | copy(items[i:], items[i+1:]) |
1 | fn main() { |
30. Parallelize execution of 1000 independent tasks
Launch the concurrent execution of procedure f with parameter i from 1 to 1000.
Tasks are independent and f(i) doesn’t return any value.
Tasks need not run all at the same time, so you may use a pool.
用参数I从1到1000启动程序f的并发执行。
任务是独立的,f(i)不返回值。
任务不需要同时运行,所以可以使用pools
1 | import "sync" |
1 | package main |
1 | use std::thread; |
or
1 | extern crate rayon; |
31. Recursive factorial (simple)
Create recursive function f which returns the factorial of non-negative integer i, calculated from f(i-1)
创建递归函数f,该函数返回从f(i-1)计算的非负整数I的阶乘
1 | func f(i int) int { |
1 | package main |
输出
1 | f(0) = 1 |
1 | fn f(n: u32) -> u32 { |
输出
24
or
1 | fn factorial(num: u64) -> u64 { |
输出
1 | 1 |
32. Integer exponentiation by squaring
Create function exp which calculates (fast) the value x power n.
x and n are non-negative integers.
创建函数exp,计算(快速)x次方n的值。
x和n是非负整数。
1 | package main |
输出
243
1 | fn exp(x: u64, n: u64) -> u64 { |
输出
65536
33. Atomically read and update variable
Assign variable x the new value f(x), making sure that no other thread may modify x between the read and the write.
为变量x分配新值f(x),确保在读和写之间没有其他线程可以修改x。
1 | package main |
6
1 | use std::sync::Mutex; |
输出
1
34. Create a set of objects
Declare and initialize a set x containing objects of type T.
声明并初始化一个包含t类型对象的集合x。
1 | x := make(map[T]bool) |
1 | package main |
1 | x contains element D |
or
1 | x := make(map[T]struct{}) |
1 | package main |
1 | x contains element B |
1 | use std::collections::HashSet; |
输出
1 | {"a", "b"} |
35. First-class function : compose
Implement a function compose (A -> C) with parameters f (A -> B) and g (B -> C), which returns composition function g ∘ f
用参数f (A -> B)和g (B -> C)实现一个函数compose (A -> C),返回composition函数g ∘ f
1 | package main |
144
1 | fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<Fn(A) -> C + 'a> |
or
1 | fn compose<A, B, C>(f: impl Fn(A) -> B, g: impl Fn(B) -> C) -> impl Fn(A) -> C { |
输出
5
36. First-class function : generic composition
Implement a function compose which returns composition function g ∘ f for any functions f and g having exactly 1 parameter.
实现一个函数组合,该函数组合为任何恰好有1个参数的函数f和g返回组合函数g ∘ f。
1 | package main |
1 | 0 2 2 |
1 | fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<Fn(A) -> C + 'a> |
or
1 | fn compose<A, B, C>(f: impl Fn(A) -> B, g: impl Fn(B) -> C) -> impl Fn(A) -> C { |
输出
5
37. Currying
Transform a function that takes multiple arguments into a function for which some of the arguments are preset.
将一个接受多个参数的函数转换为一个预设了某些参数的函数。
1 | package main |
1 | {Richissim <Jack Power> <Jean Dupont> 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0} |
1 | fn add(a: u32, b: u32) -> u32 { |
输出
17
38. Extract a substring
Find substring t consisting in characters i (included) to j (excluded) of string s.
Character indices start at 0 unless specified otherwise.
Make sure that multibyte characters are properly handled.
查找由字符串s的字符I(包括)到j(不包括)组成的子字符串t。
除非另有说明,字符索引从0开始。
确保正确处理多字节字符。
1 | package main |
utf-8 문자
1 | extern crate unicode_segmentation; |
输出
Ipsüm
or
1 | use substring::Substring; |
39. Check if string contains a word
Set boolean ok to true if string word is contained in string s as a substring, or to false otherwise.
如果字符串单词作为子字符串包含在字符串s中,则将布尔ok设置为true,否则设置为false。
1 | package main |
1 | true |
1 | fn main() { |
输出
1 | true |
原文链接: https://dashen.tech/2021/09/03/Rust-vs-Go-常用语法对比-2/
版权声明: 转载请注明出处.