-
A Singly Linked List❤️ in Rust - Ryan Levick코딩Coding/한글Rust강의★내가★공부하려고만듬 2022. 5. 25. 22:14728x90
러스트_머신머닝002_node기초_딥러닝#rust #machinelearning #deeplearning #node
https://youtu.be/XZrf69zBHGw
Double Linked List부터 할 예정
https://youtu.be/k0cL6K28SL0
Linked list
https://youtu.be/IiDHTIsmUi4
https://doc.rust-lang.org/book/ch15-00-smart-pointers.html
다른 Linked List
https://youtu.be/2q1AzGUwL7M
double linked
오류잡아야함use std::borrow::BorrowMut; use std::{ cell::RefCell, rc::{Rc, Weak}, }; #[derive(Clone)] pub struct Node<T: Copy> { pub value: T, pub next: Option<Rc<RefCell<Node<T>>>>, pub prev: Option<Weak<RefCell<Node<T>>>>, } impl<T: Copy> Node<T> { pub fn new(value: T) -> Self { Node { value, next: None, prev: None, } } } impl<T: Copy> From<Node<T>> for Option<Rc<RefCell<Node<T>>>> { fn from(node: Node<T>) -> Self { Some(Rc::new(RefCell::new(node))) } } type NodePtr<T> = Rc<RefCell<Node<T>>>; pub struct List<T: Copy> { head: Option<Node<T>>, tail: Option<Node<T>>, } impl<T: Copy> List<T> { pub fn new() -> Self { List { head: None, tail: None, } } pub fn push_back(&mut self, value: T) { let mut node = Node::new(value); match &mut self.tail.take() { None => { self.head = node.into(); self.tail = self.head.clone(); } Some(current_tail) => { node.prev = Some(Rc::downgrade(¤t_tail)); self.tail = node.into(); current_tail.borrow_mut().next = self.tail.clone(); } } } pub fn pop_back(&mut self) -> Option<T> { match &mut self.tail.take() { None => None, Some(tail) => { let mut tail = tail.borrow_mut(); let prev = tail.prev.take(); match prev { None => { self.head.take(); } Some(prev) => { let prev = prev.upgrade(); if let Some(prev) = prev { prev.borrow_mut().next = None; self.tail = Some(prev); } } }; Some(tail.value) } } } } #[cfg(test)] mod tests { use super::*; #[test] fn works_builds_list() { let mut list = List::new(); list.push_back(1); list.push_back(2); list.push_back(3); list.push_back(4); assert_eq!(list.pop_back(), Some(4)); assert_eq!(list.pop_back(), Some(3)); assert_eq!(list.pop_back(), Some(2)); assert_eq!(list.pop_back(), Some(1)); assert_eq!(list.pop_back(), None); } }
오류
cargo test Compiling rust_polyglot v0.1.0 (/Users/globalyoung/Documents/Project/Github/rust_project/rust_polyglot) error[E0308]: mismatched types --> src/lib.rs:54:48 | 54 | node.prev = Some(Rc::downgrade(¤t_tail)); | ^^^^^^^^^^^^^ expected struct `Rc`, found `&mut Node<T>` | = note: expected reference `&Rc<RefCell<Node<T>>>` found reference `&&mut Node<T>` error[E0308]: mismatched types --> src/lib.rs:56:50 | 56 | current_tail.borrow_mut().next = self.tail.clone(); | ------------------------------ ^^^^^^^^^^^^^^^^^ expected struct `Rc`, found struct `Node` | | | expected due to the type of this binding | = note: expected enum `Option<Rc<RefCell<Node<T>>>>` found enum `Option<Node<T>>` error[E0609]: no field `next` on type `&mut Rc<RefCell<Node<T>>>` --> src/lib.rs:74:47 | 74 | ... prev.borrow_mut().next = None; | ^^^^ unknown field error[E0308]: mismatched types --> src/lib.rs:75:46 | 75 | ... self.tail = Some(prev); | ^^^^ expected struct `Node`, found struct `Rc` | = note: expected struct `Node<T>` found struct `Rc<RefCell<Node<T>>>` Some errors have detailed explanations: E0308, E0609. For more information about an error, try `rustc --explain E0308`. error: could not compile `rust_polyglot` due to 4 previous errors warning: build failed, waiting for other jobs to finish... error: build failed
반응형'코딩Coding > 한글Rust강의★내가★공부하려고만듬' 카테고리의 다른 글
Recursion❤️in Programming- Full Course - freeCodeCamp.org (0) 2022.05.28 한글Rust강좌_강의_동영상❤️Understanding -Ryan Levick (0) 2022.05.26 neutral network❤️from scratch in C)러스트로 만들 예정 (0) 2022.05.26 Rust Bevy 0.7❤️- Full Tutorial - Game Development (0) 2022.05.26 Rust Node❤️연습 예정 데이터 관리-LRU Cache- Rust Programming Exercises (0) 2022.05.25 러스트에도 파이썬 넘파이와 비슷한 라이브러리가 있다?rust vs python numpy #rust #ndarray #numpy (0) 2022.05.23 Rust Error❤️Handling 외우자❤️공식 & anyhow_easyrust만세 (0) 2022.05.23 한글Rust러스트❤️015_Option_part3_Implementing_Vec_Rustonomicon (0) 2022.05.22