Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"std::vec" vs "collections::vec"

Tags:

vector

rust

Rust contains 2 identical (by api) vec modules:

http://doc.rust-lang.org/std/vec/index.html
http://doc.rust-lang.org/collections/vec/index.html

What are the differences? which is preferable to use?

like image 326
Aleksandr Avatar asked Mar 17 '23 13:03

Aleksandr


1 Answers

The collections crate is not meant to be used directly in general; you should use the std crate instead.

std::vec is just collections::vec reexported; it is exactly the same module.

If you want to use Vec, you don't even need to import it with use, because it's part of the prelude. The items defined in the prelude are always imported implicitly. If you need to import other items from that module, write use std::vec::X; instead of use collections::vec::X;


Why does collections exist? It's made available for those who write Rust applications that don't run on an operating system, or applications that are operating systems. std provides features that depend on an operating system, but some parts of std don't; those were split into smaller crates that can be reused more easily. However, those crates are not going to be stabilized in the near future, whereas std will be made stable for Rust 1.0, so unless you really need to avoid std, just use std.

You can tell the compiler that you don't want to use std by adding #![no_std] to your crate root.

like image 61
Francis Gagné Avatar answered Mar 25 '23 02:03

Francis Gagné