首页 > 其他分享 >[Rust] Use Impl with Struct

[Rust] Use Impl with Struct

时间:2024-02-19 16:14:19浏览次数:26  
标签:p2 Use p1 return let Impl str Rust result

use anyhow::{Result, anyhow};
use std::str::FromStr;

fn get_input() -> &'static str {
  return "0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2";
}

#[derive(Debug)]
struct Point {
  x: i32,
  y: i32,
}

impl FromStr for Point {
  type Err = anyhow::Error;

  fn from_str(s: &str) -> Result<Self> {
    let result = s.split_once(",");
    if result.is_none() {
      return Err(anyhow!("expected a point to contain a comma"));
    }

    let (x, y) = result.unwrap();
    let x: i32 = str::parse(x)?;
    let y: i32 = str::parse(y)?;

    return Ok(Point {x, y})
  }
}

#[derive(Debug)]
struct Line {
  p1: Point,
  p2: Point,
}

impl FromStr for Line {
  type Err = anyhow::Error;

  fn from_str(s: &str) -> Result<Self> {
      let result = s.split_once(" -> ");
      if result.is_none() {
        return Err(anyhow!("expected line contains arrow"))
      }

      let (p1, p2) = result.unwrap();
      let p1 = str::parse(p1)?;
      let p2 = str::parse(p2)?;

      return Ok(Line {p1, p2})
  }
}

impl Line {
  fn is_horv(&self) -> bool {
    return self.p1.x == self.p2.x || self.p1.y == self.p2.y
  }
}

fn main() {
  let lines = get_input()
    .lines()
    .flat_map(str::parse) // Remove Result type by using flat_map
    .filter(|x: &Line| x.is_horv())
    .collect::<Vec<Line>>();

  println!("{:?}", lines)
}

 

标签:p2,Use,p1,return,let,Impl,str,Rust,result
From: https://www.cnblogs.com/Answer1215/p/18021338

相关文章

  • djangorestframework-simplejwt 的使用
    djangorestframework-simplejwt使用转载于:https://www.cnblogs.com/liuqingzheng/p/179422271快速使用1.1配置#1安装pipinstalldjangorestframework-simplejwt#2路由层fromrest_framework_simplejwt.viewsimporttoken_obtain_pair,token_verify,token_refre......
  • 远程控制软件RustDesk自建服务器全平台部署及使用教程
    RustDesk挺出名的一款远程控制,远程协助的开源软件。完美替代TeamViewer,ToDesk,向日葵等平台。关键支持自建服务器,更安全私密远程控制电脑!其中客户端支持安卓,且支持控制安卓手机。官方地址官网:https://rustdesk.com/开源地址:https://github.com/rustdesk/一、准备工作1,有自己......
  • FileZilla 服务器 报Warning: FTP over TLS is not enabled, users cannot securely l
    FileZilla服务器报Warning:FTPoverTLSisnotenabled,userscannotsecurelylogin.1.登录至FTP服务器 2.选择编辑->设置->SSL/TLS设置->。。。。。[看图操作],注:证书导出路径不能有中文字符 3.选择编辑->设置->SSL/TLS设置->选择上一步操作导出的证书,注意导出......
  • Azul JDK 8 G1 Evacuation Pause
    PSC:\zulu-8\bin>.\java.exe-versionopenjdkversion"1.8.0_402"OpenJDKRuntimeEnvironment(Zulu8.76.0.17-CA-win64)(build1.8.0_402-b06)OpenJDK64-BitServerVM(Zulu8.76.0.17-CA-win64)(build25.402-b06,mixedmode)2024-02-18T10:3......
  • 亚马逊云ec2-user安装node-js-18.16.0
    1,下载vnm管理工具curl-o-https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh|bashexportNVM_DIR="$HOME/.nvm"[-s"$NVM_DIR/nvm.sh"]&&\."$NVM_DIR/nvm.sh"#Thisloadsnvm[-s"$NVM_DIR/bash......
  • A Simple Tutorial with Super Heroes
    2ASimpleTutorialwithSuperHeroes本章将循序渐进地介绍Voyage(对象到文档映射器)提供的各种可能性。我们将使用一个简单但并不简约的领域:超级英雄、技能及其装备。您将学习如何保存和检索对象。2.1创建连接安装好MongoBD后,我们就可以开始连接数据库了,如下所示:|repos......
  • 糟糕,被SimpleDateFormat坑到啦!| 京东云技术团队
    1.问题背景问题的背景是这样的,在最近需求开发中遇到需要将给定目标数据通过某一固定的计量规则进行过滤并打标生成明细数据,其中发现存在一笔目标数据的时间在不符合现有日期规则的条件下,还是通过了规则引擎的匹配打标操作。故而需要对该错误匹配场景进行排查,定位其根本原因所在......
  • 教你用Rust实现Smpp协议
    本文分享自华为云社区《华为云短信服务教你用Rust实现Smpp协议》,作者:张俭。协议概述SMPP(ShortMessagePeer-to-Peer)协议起源于90年代,最初由Aldiscon公司开发,后来由SMPP开发者论坛维护和推广。SMPP常用于在SMSC(ShortMessageServiceCenter,短信中心)和短信应用之间传输短消息......
  • Use Wayland with proprietary NVIDIA drivers
    Waylanddoesnotplaywellwithproprietarydrivers.CurrentlythebiggestissueisthatNVIDIAdoescurrentlynotsupportXwaylandproperly,soappsthatrequireitgetsoftwarerendering.Thisincludesmostgames,whicharethemostcommonusecasefor......
  • [Rust] Macros vs. Functions
    InRust,theexclamationmark(!)afteranameindicatesthatitisamacroratherthanafunction.MacrosandfunctionsinRustarecalleddifferentlyandservedifferentpurposes.Macros,likeanyhow!,candothingsthatfunctionscannot,suchasgenera......