0

New to the Rust language. My first crack at this is to write a program to read an EXT4 inode. Found this crate, ext4 crate, but I don't know how to import it with "use" exactly. "use ext4;" doesn't work as do "use io::ext4", "use std::ext4", etc. I installed the crate successfully with "cargo install ext4".

This is a basic question of understanding how Cargo nests its crates.

My code block that fails:

use ext4;

fn main() {
    let mut superblock = SuperBlock::new(&mut part_reader).unwrap();
    let target_inode_number = superblock.resolve_path("/etc/passwd").unwrap().inode;
    let inode = superblock.load_inode(target_inode_number).unwrap();
    let passwd_reader = superblock.open(&nice_node).unwrap();
 }

Compilation output:

   rwalk@walkubu2:~/git/rust_inode$ rustc rust_inode.rs 
error[E0432]: unresolved import `ext4`
 --> rust_inode.rs:2:5
  |
2 | use ext4;
  |     ^^^^ no `ext4` in the root

error[E0433]: failed to resolve: use of undeclared type `SuperBlock`
 --> rust_inode.rs:5:26
  |
5 |     let mut superblock = SuperBlock::new(&mut part_reader).unwrap();
  |                          ^^^^^^^^^^ use of undeclared type `SuperBlock`

error[E0425]: cannot find value `part_reader` in this scope
 --> rust_inode.rs:5:47
  |
5 |     let mut superblock = SuperBlock::new(&mut part_reader).unwrap();
  |                                               ^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `nice_node` in this scope
 --> rust_inode.rs:8:42
  |
8 |     let passwd_reader = superblock.open(&nice_node).unwrap();
  |                                          ^^^^^^^^^ not found in this scope

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0425, E0432, E0433.
For more information about an error, try `rustc --explain E0425`.
(venv_watiba) rwalk@walkubu2:~/git/rust_inode$ 
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 6
    Does this answer your question? [How do I use external crates in Rust?](https://stackoverflow.com/questions/21655032/how-do-i-use-external-crates-in-rust) – Chayim Friedman Dec 27 '21 at 20:38
  • I wasn't able to glean how to determine the nesting level of the crate from that other than to go edit the toml file directly. I suspect that would not work for more complicated crates. Thank you, but I'm hoping to learn the proper way to know how to determine where a crate lives. –  Dec 27 '21 at 21:11
  • 3
    The proper way **is to** edit the `Cargo.toml` file to tell cargo that you're using the library. Where the crate lives and if it needs to be downloaded is `cargo`'s responsibility, not yours. You shouldn't have to `cargo install` it either, just put `ext4 = "0.9.0"` in the `[dependencies]` section of the `Cargo.toml` and `cargo` will download it and allow you to use it just by referring to it's items as `ext4::SuperBlock` – Filipe Rodrigues Dec 27 '21 at 22:08
  • 1
    You can also `cargo install cargo-edit` then `cargo add`, but editing Cargo.toml is fine. – Chayim Friedman Dec 27 '21 at 22:17

0 Answers0