feat: add __network/ipnet-demo

This commit is contained in:
2023-02-26 22:51:19 +08:00
parent 9ec922f965
commit 9e06880a81
4 changed files with 43 additions and 1 deletions

View File

@@ -141,6 +141,7 @@ Project or files:
│   ├── async-speed-limit
│   ├── dingo
│   ├── ip
│   ├── ipnet-demo
│   ├── iptables
│   ├── mailsend
│   ├── message_io
@@ -269,6 +270,6 @@ Project or files:
├── vec.rs
└── while.rs
238 directories, 39 files
239 directories, 39 files
```

16
__network/ipnet-demo/Cargo.lock generated Normal file
View File

@@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "ipnet"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146"
[[package]]
name = "ipnet-demo"
version = "0.1.0"
dependencies = [
"ipnet",
]

View File

@@ -0,0 +1,9 @@
[package]
name = "ipnet-demo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ipnet = "2.7.1"

View File

@@ -0,0 +1,16 @@
use std::net::Ipv4Addr;
use std::str::FromStr;
use ipnet::{Ipv4Net, Ipv6Net};
fn main() {
let net4 = Ipv4Net::from_str("10.1.1.0/24").unwrap();
let net6 = Ipv6Net::from_str("fd00::/24").unwrap();
println!("{:?}", net4);
println!("{:?}", net6);
let ip: Ipv4Addr = "192.168.0.1".parse().unwrap();
println!("{}", net4.contains(&ip));
let ip: Ipv4Addr = "10.1.1.1".parse().unwrap();
println!("{}", net4.contains(&ip));
}