- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 371字
- 2021-07-02 23:07:20
Introduction to the standard library
To be able to understand where println! comes from, we need to take a brief look at the Rust Standard Library. If you're familiar with C, C++, or C# (or any of the other languages commonly used), you'll have used something like this:
#include <stdio.h> #include <stdlib> using System.Collections.Generic;
These are standard libraries that the compiler comes with, and which the developer can optionally include. They contain many useful procedures, functions, and methods, all designed to make development simpler so that you don't need to keep reinventing the wheel when you need to do a common task.
A similar system exists in Rust in the form of crates. The std crate contains the Rust Standard Library, and it is by default included in every other crate. This means that you can use functionality from there without extra steps.
The crates are further separated into module hierarchies, with a double colon :: being a separator for the paths. So, for example, std::fmt is the fmt module inside the std module. It contains string formatting and printing functionality. For instance, the println! macro that we have used already is there.
So why don't we have to write std::fmt::println! every time we use the println! macro? Because println! is one of the many standard macros which are imported to every namespace automatically.
You can also import things to the current namespace yourself, to save yourself some keystrokes. This is done by the use keyword. Here's an example that uses the HashMap collection type from the standard library, without using the use keyword:
let mut my_hashmap: std::collections::HashMap<String, u8> = std::collections::HashMap::new(); my_hashmap.insert("one".to_owned(), 1);
Spelling out the full namespace explicitly every time is possible, but as you can see, the noise-to-signal ratio is a bit poorer. Importing the HashMap into the current namespace can help. This piece of code is equivalent to the previous:
use std::collections::HashMap; let mut my_hashmap: HashMap<String, u8> = HashMap::new(); my_hashmap.insert("one".to_owned(), 1);
Rust's library system is a bit different from other languages, and may therefore be a bit of a stumbling block for newcomers. I found it a useful tidbit to realize that use clauses are not required to make code visible and callable: they just import a namespace into the current namespace.
- 玩轉(zhuǎn)Scratch少兒趣味編程
- Spring Cloud Alibaba核心技術(shù)與實(shí)戰(zhàn)案例
- 微服務(wù)與事件驅(qū)動(dòng)架構(gòu)
- Learning Selenium Testing Tools with Python
- PostgreSQL Cookbook
- OpenCV for Secret Agents
- Learning AndEngine
- 執(zhí)劍而舞:用代碼創(chuàng)作藝術(shù)
- C語(yǔ)言程序設(shè)計(jì)
- 基于ARM Cortex-M4F內(nèi)核的MSP432 MCU開(kāi)發(fā)實(shí)踐
- Arduino可穿戴設(shè)備開(kāi)發(fā)
- Django 5企業(yè)級(jí)Web應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)(視頻教學(xué)版)
- Python程序設(shè)計(jì)開(kāi)發(fā)寶典
- SQL Server on Linux
- 3D Printing Designs:The Sun Puzzle