182. Quine program
Output the source of the program.
输出程序的源代码
package main import "fmt" func main() { fmt.Printf("%s%c%s%c\n", s, 0x60, s, 0x60) } var s = `package main import "fmt" func main() { fmt.Printf("%s%c%s%c\n", s, 0x60, s, 0x60) } var s = `
输出:
package main import "fmt" func main() { fmt.Printf("%s%c%s%c\n", s, 0x60, s, 0x60) } var s = `package main import "fmt" func main() { fmt.Printf("%s%c%s%c\n", s, 0x60, s, 0x60) } var s = `
另一种写法:
Quine 是一种可以输出自身源码的程序。利用 go:embed 我们可以轻松实现 quine 程序:
package main import ( _ "embed" "fmt" ) //go:embed quine.go var src string func main() { fmt.Print(src) }
fn main() { let x = "fn main() {\n let x = "; let y = "print!(\"{}{:?};\n let y = {:?};\n {}\", x, x, y, y)\n}\n"; print!("{}{:?}; let y = {:?}; {}", x, x, y, y) }
输出:
fn main() { let x = "fn main() {\n let x = "; let y = "print!(\"{}{:?};\n let y = {:?};\n {}\", x, x, y, y)\n}\n"; print!("{}{:?}; let y = {:?}; {}", x, x, y, y) }
or
fn main(){print!("{},{0:?})}}","fn main(){print!(\"{},{0:?})}}\"")}
输出:
fn main(){print!("{},{0:?})}}","fn main(){print!(\"{},{0:?})}}\"")}
184. Tomorrow
Assign to variable t a string representing the day, month and year of the day after the current date.
明天的日期
import "time" t := time.Now().Add(24 * time.Hour).Format("2006-01-02")
fn main() { let t = chrono::Utc::now().date().succ().to_string(); println!("{}", t); }
185. Execute function in 30 seconds
Schedule the execution of f(42) in 30 seconds.
30秒内执行功能
import "time" timer := time.AfterFunc( 30*time.Second, func() { f(42) })
or
package main import ( "fmt" "time" ) func main() { fmt.Println("Scheduling f(42)") go func() { time.Sleep(3 * time.Second) f(42) }() // Poor man's waiting of completion of f. // Don't do this in prod, use proper synchronization instead. time.Sleep(4 * time.Second) } func f(i int) { fmt.Println("Received", i) }
use std::time::Duration; use std::thread::sleep; sleep(Duration::new(30, 0)); f(42);
186. Exit program cleanly
Exit a program cleanly indicating no error to OS
干净地退出程序
package main import ( "fmt" "os" ) func main() { fmt.Println("A") os.Exit(0) fmt.Println("B") }
or
package main import ( "fmt" "os" ) func main() { process1() process2() process3() } func process1() { fmt.Println("process 1") } func process2() { fmt.Println("process 2") defer fmt.Println("A") defer os.Exit(0) defer fmt.Println("B") fmt.Println("C") } func process3() { fmt.Println("process 3") }
process 1 process 2 C B
use std::process::exit; fn main() { println!("A"); exit(0); println!("B"); }
189. Filter and transform list
Produce a new list y containing the result of function T applied to all elements e of list x that match the predicate P.
过滤和转换列表
package main import ( "fmt" ) func P(e int) bool { // Predicate "is even" return e%2 == 0 } type Result = int func T(e int) Result { // Transformation "square" return e * e } func main() { x := []int{4, 5, 6, 7, 8, 9, 10} var y []Result for _, e := range x { if P(e) { y = append(y, T(e)) } } fmt.Println(y) }
let y = x.iter() .filter(P) .map(T) .collect::<Vec<_>>();
190. Call an external C function
Declare an external C function with the prototype
void foo(double *a, int n);
and call it, passing an array (or a list) of size 10 to a and 10 to n.
Use only standard features of your language.
调用外部C函数
// void foo(double *a, int n); // double a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; import "C" C.foo(C.a, 10)
extern "C" { /// # Safety /// /// `a` must point to an array of at least size 10 fn foo(a: *mut libc::c_double, n: libc::c_int); } let mut a = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]; let n = 10; unsafe { foo(a.as_mut_ptr(), n); }