blue_sky 0.1.1

A fun game where you guess what number the computer has chosen.
Documentation
//告诉 Rust 在另一个与模块同名的文件中加载模块的内容(rust 文件名front_of_house.rs 就是一个模块)
mod front_of_house;
//persist 目录下的mod.rs 如果不指定暴露是不能导入的
pub mod persist;
use persist::redis_operation;
mod aa;

use std::collections::HashMap;
use std::fmt::Result;
//as 处理重名问题
use std::io::Result as IoResult;
use crate::front_of_house::hosting;
pub use crate::front_of_house::hosting as hos;
//use std::cmp::Ordering;
// use std::io;
//一次性引入多个相同包或模块对象,避免多次use
use std::{cmp::Ordering, fs, io};

//所有定义引入
use std::collections::*;
use std::error::Error;

/// 文档注释,支持markdown
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = my_crate::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
    x + 1
}

pub fn eat_at_restaurant() {
    //绝对路径(倾向)
    crate::front_of_house::hosting::add_to_waitlist();
    //相对路径
    front_of_house::hosting::add_to_waitlist();
    //使用 use crate::front_of_house::hosting 简化繁琐路径
    hosting::add_to_waitlist();
    //pub use crate::front_of_house::hosting 重导出
    hos::add_to_waitlist();
    //ues 引入
    let mut map = HashMap::new();
    map.insert(1, 2);
}
pub fn save() {
    redis_operation::redis_save("a", "b");
    persist::save();
}
//------------以下是一个小示例
pub fn run(config: &Config) -> std::result::Result<(), Box<dyn Error>> {
    match fs::read_to_string(&config.filename) {
        Ok(content) => {
            println!("Content loaded:{:?}", content);
            search(&config.query, &content);
        }
        Err(e) => {
            panic!("{},file not exists!", config.filename);
        }
    }

    Ok(())
}
pub fn search(query: &str, content: &str) -> Option<String> {
    for line in content.lines() {
        if line.contains(query) {
            println!("找到:{}", line);
            return Some(line.to_string());
        }
    }
    None
}
pub struct Config {
    query: String,
    filename: String,
}
impl Config {
    pub fn new(args: &[String]) -> std::result::Result<Config, &'static str> {
        ;
        let query = args[1].clone();
        if query.len() < 3 {
            return Err("query less than 3");
        }
        let filename = args[2].clone();
        Ok(Config { query, filename })
    }
}