В
Size: a a a
K
V
VB
ВК
A
VB
V
OA
ВК
OA
VB
AZ
use crate::List::{Cons, Nil};
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug)]
enum List {
Cons(i32, RefCell<Rc<List>>),
Nil,
}
impl List {
fn tail(&self) -> Option<&RefCell<Rc<List>>> {
match self {
Cons(_, item) => Some(item),
Nil => None,
}
}
}
fn main() {
let a = Rc::new(Cons(5, RefCell::new(Rc::new(Nil))));
println!("a initial rc count = {}", Rc::strong_count(&a));
println!("a next item = {:?}", a.tail());
let b = Rc::new(Cons(10, RefCell::new(Rc::clone(&a))));
println!("a rc count after b creation = {}", Rc::strong_count(&a));
println!("b initial rc count = {}", Rc::strong_count(&b));
println!("b next item = {:?}", b.tail());
if let Some(link) = a.tail() {
*link.borrow_mut() = Rc::clone(&b);
}
println!("b rc count after changing a = {}", Rc::strong_count(&b));
println!("a rc count after changing a = {}", Rc::strong_count(&a));
// Uncomment the next line to see that we have a cycle;
// it will overflow the stack
// println!("a next item = {:?}", a.tail());
}AZ
use crate::List::{Cons, Nil};
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug)]
enum List {
Cons(i32, RefCell<Rc<List>>),
Nil,
}
impl List {
fn tail(&self) -> Option<&RefCell<Rc<List>>> {
match self {
Cons(_, item) => Some(item),
Nil => None,
}
}
}
fn main() {
let a = Rc::new(Cons(5, RefCell::new(Rc::new(Nil))));
println!("a initial rc count = {}", Rc::strong_count(&a));
println!("a next item = {:?}", a.tail());
let b = Rc::new(Cons(10, RefCell::new(Rc::clone(&a))));
println!("a rc count after b creation = {}", Rc::strong_count(&a));
println!("b initial rc count = {}", Rc::strong_count(&b));
println!("b next item = {:?}", b.tail());
if let Some(link) = a.tail() {
*link.borrow_mut() = Rc::clone(&b);
}
println!("b rc count after changing a = {}", Rc::strong_count(&b));
println!("a rc count after changing a = {}", Rc::strong_count(&a));
// Uncomment the next line to see that we have a cycle;
// it will overflow the stack
// println!("a next item = {:?}", a.tail());
}trait Node<T> {}
#[derive(Debug)]
struct Nil;
impl<T> Node<T> for Nil {}
#[derive(Debug)]
struct LinkedList<T, U: Node<T>> {
head: T,
tail: U,
}
fn nil() -> Nil {
Nil {}
}
fn cons<T, U: Node<T>>(head: T, tail: U) -> LinkedList<T, U> {
LinkedList {
head,
tail
}
}
impl<T, U: Node<T>> Node<T> for LinkedList<T, U> {}
fn main() {
let list = cons(10, cons(20, nil()));
println!("{:?}", list);
}
написал такой без рц и никаких стаковерфлоуEG
trait Node<T> {}
#[derive(Debug)]
struct Nil;
impl<T> Node<T> for Nil {}
#[derive(Debug)]
struct LinkedList<T, U: Node<T>> {
head: T,
tail: U,
}
fn nil() -> Nil {
Nil {}
}
fn cons<T, U: Node<T>>(head: T, tail: U) -> LinkedList<T, U> {
LinkedList {
head,
tail
}
}
impl<T, U: Node<T>> Node<T> for LinkedList<T, U> {}
fn main() {
let list = cons(10, cons(20, nil()));
println!("{:?}", list);
}
написал такой без рц и никаких стаковерфлоуAZ
В
trait Node<T> {}
#[derive(Debug)]
struct Nil;
impl<T> Node<T> for Nil {}
#[derive(Debug)]
struct LinkedList<T, U: Node<T>> {
head: T,
tail: U,
}
fn nil() -> Nil {
Nil {}
}
fn cons<T, U: Node<T>>(head: T, tail: U) -> LinkedList<T, U> {
LinkedList {
head,
tail
}
}
impl<T, U: Node<T>> Node<T> for LinkedList<T, U> {}
fn main() {
let list = cons(10, cons(20, nil()));
println!("{:?}", list);
}
написал такой без рц и никаких стаковерфлоуHList'ом называется... И по сути это туплAZ
AZ
HList'ом называется... И по сути это тупл