Tag:Linghu

  • Rust programming video tutorial (Advanced) – 011_ 2. Custom box

    Time:2021-10-22

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content Custom smart pointer mybox: implement deref trait//+++++++Error code+++++++++++++++++ struct MyBox<T>(T); impl<T> MyBox<T> { fn new(x: T) -> MyBox<T> { MyBox(x) } } fn main() { let x = 5; let y = MyBox::new(x); assert_eq!(5, x); assert_eq!(5, *y); } //+++++++++Correct code++++++++++++++++ //Implement deref train […]

  • Brush the first question of leetcode with rust

    Time:2021-10-21

    problem Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = […]

  • Rust programming video tutorial corresponding explanation content – Test

    Time:2021-10-20

    Video address Headline address:https://www.ixigua.com/i676544267458235648…Station B address:https://www.bilibili.com/video/av78062009?…Netease cloud classroom address:https://study.163.com/course/introduction…. Explanation content 1. Write test #[cfg(test)] mod tests { #[test] fn it_works() { assert_eq!(2 + 2, 4); } } 2. Run test cargo test 3. Using the assert macro std::assert! std::assert_eq! std::assert_ne! rust Video tutorial Trust programming language This work adoptsCC agreement, reprint must indicate the […]

  • Rust programming video tutorial (Advanced) – 012_ 1drop trail introduction

    Time:2021-10-19

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content 1. Drop trait is similar to the destructor in other languages. It executes the code of this function when the value leaves the scope.You can provide the implementation of drop trait for any type (but note that the types here need to be […]

  • Rust programming video tutorial (Advanced) – 012_ 2 call drop in advance.

    Time:2021-10-18

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content Discard the value early through STD:: MEM:: drop. When cleaning in advance is required, instead of directly calling the drop method, the STD:: MEM:: drop method is called, for example: struct Dog { name: String, } //The following is a drop trait implementation […]

  • Rust programming video tutorial (Advanced) – 013 using RC reference counting smart pointer

    Time:2021-10-17

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content 1. Consider needsB and C share the ownership of A. how to realize itExample 1: using box implementation enum List { Cons(i32, Box<List>), Nil, } use crate::List::{Cons, Nil}; fn main() { let a = Cons(5, Box::new(Cons(10, Box::new(Nil)))); let b = Cons(3, Box::new(a)); let […]

  • Rust programming video tutorial (Advanced) – 011_ 3 dereference polymorphism

    Time:2021-10-15

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content 1. Dereference forced polymorphismExample: follow the example above fn hello(name: &str) { println!(“Hello, {}!”, name); } fn main() { let m = MyBox::new(String::from(“Rust”)); hello(&m); // When dereferencing here, force polymorphism and change & string to & str. otherwise, you need: Hello (& (* […]

  • Rust programming video tutorial (Advanced) – 015_ 2 weak reference

    Time:2021-10-12

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content Creating tree data structure with weak(1) Define structure use std::rc::{Rc, Weak}; use std::cell::RefCell; #[derive(Debug)] struct Node { value: i32, parent: RefCell<Weak<Node>>, children: RefCell<Vec<Rc<Node>>>, } (2) Use example fn main() { let leaf = Rc::new(Node { value: 3, parent: RefCell::new(Weak::new()), children: RefCell::new(vec![]), }); println!(“leaf […]

  • Rust programming video tutorial (Advanced) – 016_ 3 thread and move closure

    Time:2021-10-8

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content Thread and move closure(1) Error example: use std::thread; fn main() { let v = vec![1, 2, 3]; let handle = thread::spawn(|| { println!(“Here’s a vector: {:?}”, v); }); handle.join().unwrap(); } Error reason: rust does not know how long the new thread will execute, […]

  • Rust programming video tutorial (Advanced) – 017_ 3 messaging 3

    Time:2021-10-6

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content Examples of multiple producers: use std::thread; use std::sync::mpsc; use std::time::Duration; fn main() { let (tx, rx) = mpsc::channel(); let tx1 = mpsc::Sender::clone(&tx);// Use with clone Thread:: spawn (move | {// the first sender thread let vals = vec![ String::from(“hi”), String::from(“from”), String::from(“the”), String::from(“thread”), ]; […]

  • Rust programming video tutorial (Advanced) – 018_ 2 mutex example

    Time:2021-10-4

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content 1. Mutex shared between threads(1) Error example: use std::sync::Mutex; use std::thread; fn main() { let counter = Mutex::new(0); let mut handles = vec![]; for _ in 0..10 { let handle = thread::spawn(move || { let mut num = counter.lock().unwrap(); *num += 1; }); […]

  • Rust programming video tutorial (Advanced) – 020_ 1 object

    Time:2021-10-2

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content 1. Several characteristics of object-oriented: object, encapsulation and inheritance. 2. Object(1) Object oriented programs consist of objects. An object contains data and the process of manipulating it.(2) In rust, structs and enumeration types provide methods through impl blocks. example: struct Dog { name: […]