摘要:https://cloud.tencent.com/developer/article/1620862 // use std::thread; // fn main() { // let child = thread::spawn(move || { // println!("Hello, I am
阅读全文
摘要:单元测试 测试(test)是这样一种 Rust 函数:它保证其他部分的代码按照所希望的行为正常 运行。测试函数的函数体通常会进行一些配置,运行我们想要测试的代码,然后 断言(assert)结果是不是我们所期望的。 大多数单元测试都会被放到一个叫 tests 的、带有 #[cfg(test)] 属性
阅读全文
摘要:条件编译可以通过两种不同的操作符实现,如下: - cfg属性:在属性位置中使用#[cfg(...)] - cfg!宏:在布尔表达式中使用cfg!(...) Configuration conditional checks are possible through two different oper
阅读全文
摘要:iter()通过引用遍历项目 into_iter()遍历项目,将其移至新范围 iter_mut()遍历项目,为每个项目提供可变的引用 因此,for x in my_vec { ... }本质上等效于my_vec.into_iter().for_each(|x| ... )-将my_vec的两个元素都
阅读全文
摘要:fn consume_with_relish<F>(mut func: F) where F: FnMut() -> String { // `func` consumes its captured variables, so it cannot be run more // than once p
阅读全文
摘要:https://blog.csdn.net/guiqulaxi920/article/details/78823541 fn main() { // Basic Range (exclusive on the right) for i in 1..11 { print!("{} ", i); } p
阅读全文
摘要:https://github.com/rustomax/rust-iterators 1、迭代器是什么? 迭代器(iterator)负责遍历序列中的每一项和决定序列何时结束的逻辑,迭代器是 惰性的(lazy)。迭代器模式允许你对一个项的序列进行某些处理。 let v = vec![1, 2, 3];
阅读全文
摘要:use futures::{ self, executor}; use std::thread; use std::time; async fn song() { loop { println!("song!"); thread::sleep(time::Duration::from_secs(5)
阅读全文
摘要:use std::sync::Arc; let foo = Arc::new(vec![1.0, 2.0, 3.0]); // The two syntaxes below are equivalent. let a = foo.clone(); let b = Arc::clone(&foo);
阅读全文
摘要:use std::thread; use std::time; use std::sync::mpsc; fn main() { let (tx, rx) : (mpsc::Sender<i32>, mpsc::Receiver<i32>) = mpsc::channel(); //drop(rx)
阅读全文
摘要:fn main() { let s1 = String::from("hello"); let s2 = s1; println!("s1 : {}", s1); let s3 = s2.clone(); println!("s2 : {}", s2); println!("s3 : {}", s3
阅读全文
摘要:use std::sync::Mutex; fn main() { let m = Mutex::new(5); //Mutex<i32>, 智能指针 { let mut num = m.lock().unwrap(); //等待,阻塞线程, //返回MutexGuard, 智能指针,实施deref
阅读全文
摘要:The Atomic Reference Counter (Arc) type is a smart pointer that lets you share immutable data across threads in a thread-safe way. I couldn’t find any
阅读全文
摘要:Blog series Part 1: Introduction & Allocators Part 2: Unq, an allocator-aware Box Part 3: Containers Part 4: Generating Vulkan bindings Welcome to Han
阅读全文
摘要:use std::ops::Deref; struct MyBox<T>(T); impl<T> MyBox<T> { fn new(x: T) -> MyBox<T> { MyBox(x) } } //impl<T> Deref for MyBox<T> { // type Target = T;
阅读全文
摘要:https://kaisery.github.io/trpl-zh-cn/ch04-02-references-and-borrowing.html 示例 4-5 中的元组代码有这样一个问题:我们必须将 String 返回给调用函数,以便在调用 calculate_length 后仍能使用 Stri
阅读全文
摘要:struct Rec { width : u32, length : u32 } fn process(rec1: Rec) -> Rec { let mut rec2 = rec1; rec2.width = 10; rec2.length = 11; rec2 } fn main() { let
阅读全文
摘要:[root@bogon fnmut]# cat src/main.rs #[derive(Debug)] struct f_closure{ name: String, } impl f_closure{ fn fn_call( self) -> String{ self.name } } fn g
阅读全文
摘要:cat src/main.rs #[derive(Debug)] struct f_closure{ name: String, } impl f_closure{ fn fn_call( self) -> String{ self.name } } fn get_string<T>(name: S
阅读全文
摘要:cat src/main.rs #[derive(Debug)] struct f_closure{ name: String, } impl f_closure{ fn fn_call(& self) -> String{ self.name } } fn main() { let name =
阅读全文