mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-22 06:03:27 +08:00
Updated 'src/smart_pointers/refcell-t'.
This commit is contained in:
6
projects/borrowing/Cargo.toml
Normal file
6
projects/borrowing/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "borrowing"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
4
projects/borrowing/src/main.rs
Normal file
4
projects/borrowing/src/main.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
let x = 5;
|
||||
let y = &mut x;
|
||||
}
|
||||
@@ -1,12 +1,24 @@
|
||||
#[derive(Debug)]
|
||||
enum List {
|
||||
Cons(i32, Box<List>),
|
||||
Cons(Rc<RefCell<i32>>, Rc<List>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
use crate::List::{Cons, Nil};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn main() {
|
||||
let a = Cons(5, Box::new(Cons(10, Box::new(Nil))));
|
||||
let b = Cons(3, Box::new(a));
|
||||
let c = Cons(4, Box::new(a));
|
||||
let value = Rc::new(RefCell::new(5));
|
||||
|
||||
let a = Rc::new(Cons(Rc::clone(&value), Rc::new(Nil)));
|
||||
|
||||
let b = Cons(Rc::new(RefCell::new(3)), Rc::clone(&a));
|
||||
let c = Cons(Rc::new(RefCell::new(4)), Rc::clone(&a));
|
||||
|
||||
*value.borrow_mut() += 10;
|
||||
|
||||
println!("a 随后 = {a:?}");
|
||||
println!("b 随后 = {b:?}");
|
||||
println!("c 随后 = {c:?}");
|
||||
}
|
||||
|
||||
6
projects/limit-tracker/Cargo.toml
Normal file
6
projects/limit-tracker/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "limit-tracker"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -25,15 +25,19 @@ where
|
||||
|
||||
let percentage_of_max = self.value as f64 / self.max as f64;
|
||||
|
||||
match percentage_of_max {
|
||||
p if p >= 1.0 => self.messenger.send("出错:你已超出你的配额!"),
|
||||
p if p >= 0.9 => self.messenger.send("紧急警告:你已用掉你配额的 90% !"),
|
||||
p if p >= 0.75 => self.messenger.send("警告:你已用掉你配额的 75% !"),
|
||||
_ => {},
|
||||
}
|
||||
if percentage_of_max >= 1.0 {
|
||||
self.messenger.send("Error: 你已超出配额!");
|
||||
} else if percentage_of_max >= 0.9 {
|
||||
self.messenger
|
||||
.send("Urgent warning: 你已用完 90% 的配额!");
|
||||
} else if percentage_of_max >= 0.75 {
|
||||
self.messenger
|
||||
.send("Warning: 你已用完 75% 的配额!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -46,25 +50,28 @@ mod tests {
|
||||
impl MockMessenger {
|
||||
fn new() -> MockMessenger {
|
||||
MockMessenger {
|
||||
sent_messages: RefCell::new(vec! []),
|
||||
sent_messages: RefCell::new(vec![]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Messenger for MockMessenger {
|
||||
fn send(&self, message: &str) {
|
||||
self.sent_messages.borrow_mut().push(String::from(message));
|
||||
let mut one_borrow = self.sent_messages.borrow_mut();
|
||||
let mut two_borrow = self.sent_messages.borrow_mut();
|
||||
|
||||
one_borrow.push(String::from(message));
|
||||
two_borrow.push(String::from(message));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_sends_an_over_75_percent_waring_message() {
|
||||
fn it_sends_an_over_75_percent_warning_message() {
|
||||
let mock_messenger = MockMessenger::new();
|
||||
let mut limit_tracker = LimitTracker::new(&mock_messenger, 100);
|
||||
|
||||
limit_tracker.set_value(80);
|
||||
println! ("{}", mock_messenger.sent_messages.borrow().iter().next().unwrap());
|
||||
|
||||
assert_eq! (mock_messenger.sent_messages.borrow().len(), 1);
|
||||
assert_eq!(mock_messenger.sent_messages.borrow().len(), 1);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "limit_tracker"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
Reference in New Issue
Block a user