101. Load from HTTP GET request into a string
Make an HTTP request with method GET to URL u, then store the body of the response in string s.
发起http请求
1 | package main |
res has type *http.Response.
buffer has type []byte.
It is idiomatic and strongly recommended to check errors at each step.
GET response: 200 Hello Inigo Montoya
1 | extern crate reqwest; |
or
1 | [dependencies] |
or
1 | [dependencies] |
102. Load from HTTP GET request into a file
Make an HTTP request with method GET to URL u, then store the body of the response in file result.txt. Try to save the data as it arrives if possible, without having all its content in memory at once.
发起http请求
1 | package main |
resp has type *http.Response.
It is idiomatic and strongly recommended to check errors at each step, except for the calls to Close.
1 | Making GET request |
1 | extern crate reqwest; |
105. Current executable name
Assign to string s the name of the currently executing program (but not its full path).
当前可执行文件名称
将当前正在执行的程序的名称分配给字符串s(但不是它的完整路径)。
1 | package main |
play
1 | fn get_exec_name() -> Option<String> { |
playground
or
1 | fn main() { |
playground
106. Get program working directory
Assign to string dir the path of the working directory.
(This is not necessarily the folder containing the executable itself)
获取程序的工作路径
1 | package main |
/ <nil>
1 | use std::env; |
"/playground"
107. Get folder containing current program
Assign to string dir the path of the folder containing the currently running executable.
(This is not necessarily the working directory, though.)
获取包含当前程序的文件夹
1 | package main |
/tmpfs
1 | let dir = std::env::current_exe()? |
Rust doesn’t represent paths as Strings, so we need to convert the Path returned from Path::parent. This code chooses to do this lossily, replacing characters it doesn’t recognize with �
109. Number of bytes of a type
Set n to the number of bytes of a variable t (of type T).
获取某个类型的字节数
1 | package main |
A main.T object is 24 bytes.
1 | // T has (8 + 4) == 12 bytes of data |
16 bytes
110. Check if string is blank
Set boolean blank to true if string s is empty, or null, or contains only whitespace ; false otherwise.
检查字符串是否空白
1 | package main |
1 | "" is blank |
1 | fn main() { |
1 | "" is blank |
111. Launch other program
From current process, run program x with command-line parameters “a”, “b”.
运行其他程序
1 | package main |
exec: "x": executable file not found in $PATH
1 | use std::process::Command; |
1 | X11 |
or
1 | use std::process::Command; |
or
1 | use std::process::Command; |
112. Iterate over map entries, ordered by keys
Print each key k with its value x from an associative array mymap, in ascending order of k.
遍历map,按key排序
1 | package main |
1 | Key = four , Value = 4 |
1 | use std::collections::BTreeMap; |
1 | (five, 5) |
113. Iterate over map entries, ordered by values
Print each key k with its value x from an associative array mymap, in ascending order of x.
Note that multiple entries may exist for the same value x.
遍历map,按值排序
1 | package main |
1 | Key = one , Value = 1 |
or
1 | package main |
1 | Key = one , Value = 1 |
1 | use itertools::Itertools; |
1 | [4,1] |
or
1 | use std::collections::HashMap; |
1 | [4,1] |
114. Test deep equality
Set boolean b to true if objects x and y contain the same values, recursively comparing all referenced elements in x and y.
Tell if the code correctly handles recursive types.
深度判等
1 | package main |
1 | x doesn't deep equal list[ 0 ] |
1 | let b = x == y; |
The == operator can only be used by having a type implement PartialEq.
115. Compare dates
Set boolean b to true if date d1 is strictly before date d2 ; false otherwise.
日期比较
1 | package main |
true
1 | extern crate chrono; |
116. Remove occurrences of word from string
Remove all occurrences of string w from string s1, and store the result in s2.
去除指定字符串
1 | package main |
bar
1 | fn main() { |
bar
or
1 | fn main() { |
bar
117. Get list size
获取list的大小
1 | package main |
1 | 3 |
1 | fn main() { |
x has 3 elements
118. List to set
Create set y from list x.
x may contain duplicates. y is unordered and has no repeated values.
从list到set
1 | package main |
1 | x = [b a b c] |
1 | use std::collections::HashSet; |
1 | x: [1, 7, 3, 1] |
119. Deduplicate list
Remove duplicates from list x.
Explain if original order is preserved.
list去重
1 | package main |
1 | x = [b a b c] |
or
1 | package main |
1 | x = [b a b b c b a] |
or
1 | package main |
1 | 22, 11, 22, 22, 33, 22, 11, 11 |
1 | fn main() { |
[1, 2, 3, 4]
or
1 | use itertools::Itertools; |
[1, 2, 3, 4]
120. Read integer from stdin
Read an integer value from the standard input into variable n
从标准输入中读取整数
1 | package main |
1 | 42 <nil> |
or
1 | package main |
1 | 42 <nil> |
1 | fn get_input() -> String { |
or
1 | use std::io; |
or
1 | use std::io::BufRead; |
原文链接: https://dashen.tech/2021/09/07/Rust-vs-Go-常用语法对比-6/
版权声明: 转载请注明出处.