Kaynağa Gözat

Use strip_prefix function

(The 'manual_strip' Clippy lint)
Benjamin Sago 4 yıl önce
ebeveyn
işleme
5eacfd9e5c
2 değiştirilmiş dosya ile 8 ekleme ve 9 silme
  1. 5 5
      src/options.rs
  2. 3 4
      src/resolve.rs

+ 5 - 5
src/options.rs

@@ -213,9 +213,9 @@ impl Inputs {
 
     fn load_free_args(&mut self, matches: getopts::Matches) -> Result<(), OptionsError> {
         for a in matches.free {
-            if a.starts_with('@') {
-                trace!("Got nameserver -> {:?}", &a[1..]);
-                self.add_nameserver(&a[1..])?;
+            if let Some(nameserver) = a.strip_prefix('@') {
+                trace!("Got nameserver -> {:?}", nameserver);
+                self.add_nameserver(nameserver)?;
             }
             else if a.chars().all(char::is_uppercase) {
                 if let Some(class) = self.parse_class_name(&a) {
@@ -273,8 +273,8 @@ impl TxidGenerator {
 }
 
 fn parse_dec_or_hex(input: &str) -> Option<u16> {
-    if input.starts_with("0x") {
-        match u16::from_str_radix(&input[2..], 16) {
+    if let Some(hex_str) = input.strip_prefix("0x") {
+        match u16::from_str_radix(hex_str, 16) {
             Ok(num) => {
                 Some(num)
             }

+ 3 - 4
src/resolve.rs

@@ -36,12 +36,11 @@ impl Resolver {
                 for line in reader.lines() {
                     let line = line?;
 
-                    if line.starts_with("nameserver ") {
-                        let line = &line[11..];
-                        let ip: Result<std::net::Ipv4Addr, _> = line.parse();
+                    if let Some(nameserver_str) = line.strip_prefix("nameserver ") {
+                        let ip: Result<std::net::Ipv4Addr, _> = nameserver_str.parse();
 
                         match ip {
-                            Ok(_ip) => nameservers.push(line.into()),
+                            Ok(_ip) => nameservers.push(nameserver_str.into()),
                             Err(e)  => warn!("Failed to parse nameserver line {:?}: {}", line, e),
                         }
                     }