Rust4.1 Managing Growing Projects with Packages, Crates, and Modules

这篇具有很好参考价值的文章主要介绍了Rust4.1 Managing Growing Projects with Packages, Crates, and Modules。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Rust学习笔记

Rust编程语言入门教程课程笔记

参考教材: The Rust Programming Language (by Steve Klabnik and Carol Nichols, with contributions from the Rust Community)

Lecture 7: Managing Growing Projects with Packages, Crates, and Modules

src/main.rs

// src/main.rs: the main file of the project, where the main function is defined; this is the crate root
// src/lib.rs: the root of your crate’s library; the library’s name is the same as the name of the crate
// src/bin: directory that can contain multiple binary crates; each file in this directory will be a separate binary crate
// src/bin/main.rs: the main file of the binary crate with the same name as the directory; this file is the crate root of the binary crate

//Modules
use std::collections::HashMap;// use keyword to bring module into scope

use std::io::Result as IoResult;// use keyword to bring module into scope
use std::{cmp, io};// use keyword to bring module into scope
use rand::Rng;// use keyword to bring module into scope
use std::collections::*;// use keyword to bring module into scope, * is glob operator

fn main() {
    let mut map = HashMap::new();
    map.insert(1, 2);
    println!("{:?}", map);
    let m = IoResult::Ok(());
    println!("{:?}", m);

    let mut rng = rand::thread_rng();
    let t = rng.gen_range(1..=10);
    println!("{:?}", t);
}

src/lib.rs

mod front_of_house;//{// module

    // pub mod hosting{// public module
    //     pub fn add_to_waitlist(){}
    // }

    // mod serving{// private module
    //     fn take_order(){}
    //     fn serve_order(){}
    //     fn take_payment(){}
    // }

    // fn fix_incorrect_order(){
    //     cook_order();
    //     super::serve_order();// super keyword to access parent module
    //     crate::serve_order();// absolute path
    // }

    // pub fn cook_order(){}

    // pub struct Breakfast{
    //     pub toast: String,
    //     seasonal_fruit: String,
    // }

    // impl Breakfast{
    //     pub fn summer(toast: &str) -> Breakfast{
    //         Breakfast{
    //             toast: String::from(toast),
    //             seasonal_fruit: String::from("peaches"),
    //         }
    //     }
    // }
//}

pub use crate::front_of_house::hosting;// use keyword to bring module into scope
//use crate::front_of_house::servering;// cannot use private module
//use front_of_house::hosting;// relative path

pub fn eat_at_restaurant(){
    // Absolute path
    crate::front_of_house::hosting::add_to_waitlist();

    // Relative path
    front_of_house::hosting::add_to_waitlist();

    // Order a breakfast in the summer with Rye toast
    let mut meal = front_of_house::Breakfast::summer("Rye");
    // Change our mind about what bread we'd like
    meal.toast = String::from("Wheat");
    println!("I'd like {} toast please", meal.toast);
    // The next line won't compile if we uncomment it; we're not allowed
    // to see or modify the seasonal fruit that comes with the meal
    // meal.seasonal_fruit = String::from("blueberries");

    hosting::add_to_waitlist();

}

fn serve_order(){}

src/front_of_house.rs

pub mod hosting; //{// public module
//     pub fn add_to_waitlist(){}
// }

mod serving{// private module
    fn take_order(){}
    fn serve_order(){}
    fn take_payment(){}
}

fn fix_incorrect_order(){
    cook_order();
    super::serve_order();// super keyword to access parent module
    crate::serve_order();// absolute path
}

pub fn cook_order(){}

pub struct Breakfast{
    pub toast: String,
    seasonal_fruit: String,
}

impl Breakfast{
    pub fn summer(toast: &str) -> Breakfast{
        Breakfast{
            toast: String::from(toast),
            seasonal_fruit: String::from("peaches"),
        }
    }
}

src/front_of_house/hosting.rs文章来源地址https://www.toymoban.com/news/detail-760137.html

pub fn add_to_waitlist(){}

到了这里,关于Rust4.1 Managing Growing Projects with Packages, Crates, and Modules的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【跟小嘉学 Rust 编程】十四、关于 Cargo 和 Crates.io

    【跟小嘉学 Rust 编程】一、Rust 编程基础 【跟小嘉学 Rust 编程】二、Rust 包管理工具使用 【跟小嘉学 Rust 编程】三、Rust 的基本程序概念 【跟小嘉学 Rust 编程】四、理解 Rust 的所有权概念 【跟小嘉学 Rust 编程】五、使用结构体关联结构化数据 【跟小嘉学 Rust 编程】六、枚举

    2024年02月11日
    浏览(38)
  • Python Packages for Big Data Analysis and Visualization

    作者:禅与计算机程序设计艺术 Python第三方库主要分为两类:数据处理、可视化。下面是用于大数据分析与可视化的常用的Python第三方库列表(按推荐顺序排序): NumPy: NumPy 是用 Python 编写的一个科学计算库,其功能强大且全面,尤其适用于对大型多维数组和矩阵进行快速

    2024年02月07日
    浏览(37)
  • vscode中 rust-analyzer插件报错 failed to find any projects in [AbsPathBuf 或者FetchWorkspaceError的解决办法

    一般来说,如果vscode打开的是rust项目的根目录(即目录下有 Cargo.toml 和 src/ 文件),rust-analyzer插件可以识别到项目。 而如果rust项目只是vscode资源管理器打开的子目录,则有时会出现failed to find any projects in [AbsPathBuf xxx或者FetchWorkspaceError的报错,rust-analyzer也无法正常运行,如

    2024年02月19日
    浏览(37)
  • Low Cost and High Performance FPGA with ARM and SDRAM inside

    AG10KSDE176 AGM AG10KSDE176 是由 AGM FPGA AG10K 与 SDRAM 叠封集成的芯片,具有 AG10K FPGA 的可编程功能,提供更多可编程 IO,同时内部连接大容量 SDRAM。  FPGA 外部管脚输出 EQFP176 封装底部 Pad 为 GND,管脚说明请见下表:  SDRAM 说明 内部 SDRAM 为 64Mbit(512K words × 4 banks × 32 bits)容量

    2024年02月04日
    浏览(37)
  • 【跟小嘉学 Rust 编程】十八、模式匹配(Patterns and Matching)

    【跟小嘉学 Rust 编程】一、Rust 编程基础 【跟小嘉学 Rust 编程】二、Rust 包管理工具使用 【跟小嘉学 Rust 编程】三、Rust 的基本程序概念 【跟小嘉学 Rust 编程】四、理解 Rust 的所有权概念 【跟小嘉学 Rust 编程】五、使用结构体关联结构化数据 【跟小嘉学 Rust 编程】六、枚举

    2024年02月11日
    浏览(30)
  • Office Tool with runtime and Office 2021

    Office_Tool_Plus_16.0.14332.20481 64-bit.iso Office LTSC Professional Plus 2021 - Volume License Project Professional 2021 - Volume License Visio LTSC Professional 2021 - Volume License 迅雷云盘 迅雷云盘 https://pan.xunlei.com/s/VNRcfM43uH0i4UcPhguxWF4hA1 链接:https://pan.xunlei.com/s/VNRcfM43uH0i4UcPhguxWF4hA1 提取码:23bi 复制这段内容

    2024年02月04日
    浏览(42)
  • Rust generic type parameter and `impl Trait` parameter

    (Jin Qing’s Column, Jun., 2023) See: https://doc.rust-lang.org/reference/types/impl-trait.html Generic type parameter and impl Trait parameter are almost equivalent: impl Trait is just a syntactic sugar for generic type parameter. It is a little simpler by omitting the generic parameters ... . But they are not exactly equivalent. The caller can use a gener

    2024年02月09日
    浏览(23)
  • Stream Processing with Apache Storm and Hadoop

    大数据时代,实时数据处理成为了企业和组织的关注之一。随着互联网的发展,数据量越来越大,传统的批处理方式无法满足实时需求。因此,流处理技术逐渐成为了关注的焦点。 Apache Storm是一个开源的流处理系统,可以处理大量实时数据。它具有高吞吐量、低延迟和可扩展

    2024年04月17日
    浏览(29)
  • 2 Data Streaming Pipelines With Flink and Kafka

    作者:禅与计算机程序设计艺术 数据流是一个连续不断的、产生、存储和处理数据的过程。传统上,数据流编程都是基于特定平台(比如:消息队列,数据仓库,事件溯源)的SDK或者API进行开发,但随着云计算和容器技术的发展,越来越多的企业选择使用开源工具实现自己的

    2024年02月08日
    浏览(44)
  • 第九题: Deploying PHP Applications with Docker Compose and

    作者:禅与计算机程序设计艺术 随着云计算、容器化应用的流行,开发者越来越喜欢使用云平台部署自己的应用,特别是在微服务架构越来越普及的时代。云平台提供的按需伸缩、自动弹性伸缩、负载均衡等资源管理功能,可以让应用的开发和运维效率得到提升。目前市面上

    2024年02月08日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包