当前位置: 首页 > news >正文

【rust】rust基础代码案例

文章目录

  • 代码篇
    • HelloWorld
    • 斐波那契数列
    • 计算表达式(加减乘除)
    • web接口
  • 优化篇
    • target/目录占用一个g,仅仅一个actix的helloWorld demo
    • 升级rust版本, 通过rustup
    • cargo换源
    • windows下放弃吧,需要额外安装1g的toolchain并且要配合msvc(10g起步吧)或者mingw。

代码篇

HelloWorld

fn main() {print!("Hello,Wolrd")
}

斐波那契数列

fn fib(n: u64) -> u64 {let mut a = 0;let mut b = 1;for _ in 0..n {let c = a + b;a = b;b = c;}a
}fn main() {let n = 10;let result_list: Vec<String> = (1..n+1).map(|i| fib(i).to_string()).collect();let result = result_list.join(",");println!("{}", result);
}

计算表达式(加减乘除)

use std::io;
use std::num::ParseIntError;
fn parse_expression(expression: &str) -> Result<i32, ParseIntError> {let mut result = 0;let mut current_number = 0;let mut operator = '+';for token in expression.split_whitespace() {match token {"+" => operator = '+',"-" => operator = '-',"*" => operator = '*',"/" => operator = '/',_ => {current_number = match token.parse::<i32>() {Ok(num) => num,Err(e) => return Err(e),};match operator {'+' => result += current_number,'-' => result -= current_number,'*' => result *= current_number,'/' => result /= current_number,_ => {},}}}}Ok(result)
}
fn main() {println!("请输入表达式(例如:1 + 2 * 3):");let mut input = String::new();io::stdin().read_line(&mut input).expect("读取输入失败");let expression = input.trim();match parse_expression(expression) {Ok(result) => println!("计算结果为:{}", result),Err(e) => println!("解析表达式时出错:{}", e),}
}

web接口

use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};#[get("/")]
async fn hello() -> impl Responder {HttpResponse::Ok().body("Hello world!")
}#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {HttpResponse::Ok().body(req_body)
}async fn manual_hello() -> impl Responder {HttpResponse::Ok().body("Hey there!")
}#[actix_web::main]
async fn main() -> std::io::Result<()> {HttpServer::new(|| {App::new().service(hello).service(echo).route("/hey", web::get().to(manual_hello))}).bind(("127.0.0.1", 8080))?.run().await
}

优化篇

target/目录占用一个g,仅仅一个actix的helloWorld demo

~/.cargo/config写入, 放到项目之外去,眼不见心不烦

[build]
target-dir = "/path/rust-target"

在这里插入图片描述
不过一个demo 接口, 你执行下cargo run 和 cargo build --release, 就占用1.2g。 这可真烧磁盘, 有node_module那味儿了。

升级rust版本, 通过rustup

export RUSTUP_DIST_SERVER="https://rsproxy.cn"
export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
rustup update

cargo换源

~/.cargo/config写入

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index/"

windows下放弃吧,需要额外安装1g的toolchain并且要配合msvc(10g起步吧)或者mingw。

在这里插入图片描述
比linux下的工具链大多了
在这里插入图片描述

http://www.lryc.cn/news/479680.html

相关文章:

  • 【深度学习】PromptFix:多功能AI修图
  • 2024最新AI绘画系统软件(Midjourney)+GPT4文档分析总结,多模态识图理解,AI文生图/图生图/混图生图(图像混合)
  • 【信号处理】基于联合图像表示的深度学习卷积神经网络
  • C#基础-区分数组与集合
  • ORACLE 19C 安装数据库补丁的详细过程
  • tensorflow案例5--基于改进VGG16模型的马铃薯识别,准确率提升0.6%,计算量降低78.07%
  • 代码中的设计模式-策略模式
  • 后端Node学习项目-项目基础搭建
  • Python | Leetcode Python题解之第538题把二叉搜索树转换为累加树
  • 【ZeroMQ 】ZeroMQ中inproc优势有哪些?与其它传输协议有哪些不同?
  • spark的学习-03
  • 一文了解Android SELinux
  • 数据血缘追踪是如何在ETL过程中发挥作用?
  • 跟我学C++中级篇——生产中如何调试程序
  • Python爬虫实战 | 爬取网易云音乐热歌榜单
  • apk因检测是否使用代理无法抓包绕过方式
  • DevOps业务价值流:架构设计最佳实践
  • 计算机网络——SDN
  • 开源数据库 - mysql - innodb源码阅读 - master线程(一)
  • vscode ssh连接autodl失败
  • 文件系统和日志管理 附实验:远程访问第一台虚拟机日志
  • 云上拼团GO指南——腾讯云博客部署案例,双11欢乐GO
  • 【VScode】VScode内的ChatGPT插件——CodeMoss全解析与实用教程
  • 水库大坝安全监测预警方法
  • 深度学习:微调(Fine-tuning)详解
  • qt QWebSocketServer详解
  • 【数据结构】线性表——链表
  • Fork突然报错
  • Vue Element-UI 选择隐藏表格中的局部字段信息
  • easyui +vue v-slot 注意事项