#

当前标签共计9篇文章。

mac m1上使用rust编译openssl-sys失败的一次记录

2022-04-25 20:43:14
93

mac m1 编译openssl-sys的时候报错: 遇到错误,报错信息如下 ``` admin@admindeMacBook-Air b3 % cargo run Compiling openssl-sys v0.9.72 error: failed to run custom build command for `openssl-sys v0.9.72` Caused by: process didn't exit successfully: `/Users/admin/project/b3/target/debug/build/openssl-sys-7ab7e6047ef6ec59/build-script-main` (exit status: 101) --- stdout cargo:rustc-cfg=const_fn cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR unset cargo:rerun-if-env-changed=OPENSSL_LIB_DIR OPENSSL_LIB_DIR unset cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR unset cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR OPENSSL_INCLUDE_DIR unset cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_DIR AARCH64_APPLE_DARWIN_OPENSSL_DIR unset cargo:rerun-if-env-changed=OPENSSL_DIR OPENSSL_DIR unset cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG cargo:rerun-if-env-changed=PKG_CONFIG_aarch64-apple-darwin cargo:rerun-if-env-changed=PKG_CONFIG_aarch64_apple_darwin cargo:rerun-if-env-changed=HOST_PKG_CONFIG cargo:rerun-if-env-changed=PKG_CONFIG cargo:rerun-if-env-changed=OPENSSL_STATIC cargo:rerun-if-env-changed=OPENSSL_DYNAMIC cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC cargo:rerun-if-env-changed=PKG_CONFIG_PATH_aarch64-apple-darwin cargo:rerun-if-env-changed=PKG_CONFIG_PATH_aarch64_apple_darwin cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH cargo:rerun-if-env-changed=PKG_CONFIG_PATH cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_aarch64-apple-darwin cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_aarch64_apple_darwin cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64-apple-darwin cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64_apple_darwin cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR run pkg_config fail: "Could not run `\"pkg-config\" \"--libs\" \"--cflags\" \"openssl\"`\nThe pkg-config command could not be found.\n\nMost likely, you need to install a pkg-config package for your OS.\nTry `brew install pkg-config` if you have Homebrew.\n\nIf you've already installed it, ensure the pkg-config command is one of the\ndirectories in the PATH environment variable.\n\nIf you did not expect this build to link to a pre-installed system library,\nthen check documentation of the openssl-sys crate for an option to\nbuild the library from source, or disable features or dependencies\nthat require pkg-config." --- stderr thread 'main' panicked at ' Could not find directory of OpenSSL installation, and this `-sys` crate cannot proceed without this knowledge. If OpenSSL is installed and this crate had trouble finding it, you can set the `OPENSSL_DIR` environment variable for the compilation process. Make sure you also have the development packages of openssl installed. For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora. If you're in a situation where you think the directory *should* be found automatically, please open a bug at https://github.com/sfackler/rust-openssl and include information about your system as well as this message. $HOST = aarch64-apple-darwin $TARGET = aarch64-apple-darwin openssl-sys = 0.9.72 ', /Users/admin/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/openssl-sys-0.9.72/build/find_normal.rs:180:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` 同样的代码在ubuntu上正常,但是在mac m1上面无法编译,根据报错信息所以猜测是openssl-sys这个库需要调用本机上的openssl,但是无法找到。 找到了一篇文档, https://reality0ne.com/macos-xia-shi-yong-rust-openssl/ 执行文章中的命令: ``` $ cargo clean $ export OPENSSL_LIB_DIR=$(brew --prefix openssl)/lib $ export OPENSSL_INCLUDE_DIR=$(brew --prefix openssl)/include $ cargo build $ cargo test ``` 然后到项目里面cargo run一下,仍然报错 ``` admin@admindeMacBook-Air b3 % cargo run Compiling openssl-sys v0.9.72 Compiling socket2 v0.4.3 Compiling memmap2 v0.5.3 Compiling rand v0.4.6 Compiling memmap v0.7.0 Compiling quote v1.0.15 Compiling twoway v0.1.8 Compiling buf_redux v0.8.4 error: failed to run custom build command for `openssl-sys v0.9.72` Caused by: process didn't exit successfully: `/Users/admin/project/b3/target/debug/build/openssl-sys-7ab7e6047ef6ec59/build-script-main` (exit status: 101) --- stdout cargo:rustc-cfg=const_fn cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR unset cargo:rerun-if-env-changed=OPENSSL_LIB_DIR OPENSSL_LIB_DIR = /opt/homebrew/opt/openssl@3/lib cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR unset cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR OPENSSL_INCLUDE_DIR = /opt/homebrew/opt/openssl@3/include --- stderr thread 'main' panicked at 'OpenSSL library directory does not exist: /opt/homebrew/opt/openssl@3/lib', /Users/admin/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/openssl-sys-0.9.72/build/main.rs:68:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace warning: build failed, waiting for other jobs to finish... ``` 这个提示大概看懂了,意思是openssl的库在/opt/homebrew/opt/openssl@3/lib下不存在,cd到这个目录之后发现确实不存在 ``` admin@admindeMacBook-Air opt % ls pkg-config pkgconfig ``` 所以打算用homebrew重新安装一下,这里使用了这个作者的方法 https://zhuanlan.zhihu.com/p/111014448 (homebrew国内镜像安装) 然后执行`brew install openssl` 执行过程: ``` admin@admindeMacBook-Air opt % brew install openssl Error: homebrew-core is a shallow clone. homebrew-cask is a shallow clone. To `brew update`, first run: git -C /opt/homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow git -C /opt/homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow These commands may take a few minutes to run due to the large size of the repositories. This restriction has been made on GitHub's request because updating shallow clones is an extremely expensive operation due to the tree layout and traffic of Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you automatically to avoid repeatedly performing an expensive unshallow operation in CI systems (which should instead be fixed to not use shallow clones). Sorry for the inconvenience! ==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/ca-certificates-2022-03-29.all.bottle.tar.gz ######################################################################## 100.0% ==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/openssl%403-3.0.2.arm64_big_sur.bottle.tar.gz ######################################################################## 100.0% ==> Installing dependencies for openssl@3: ca-certificates ==> Installing openssl@3 dependency: ca-certificates ==> Pouring ca-certificates-2022-03-29.all.bottle.tar.gz ==> Regenerating CA certificate bundle from keychain, this may take a while... 🍺 /opt/homebrew/Cellar/ca-certificates/2022-03-29: 3 files, 211.5KB ==> Installing openssl@3 ==> Pouring openssl@3-3.0.2.arm64_big_sur.bottle.tar.gz ==> Caveats A CA file has been bootstrapped using certificates from the system keychain. To add additional certificates, place .pem files in /opt/homebrew/etc/openssl@3/certs and run /opt/homebrew/opt/openssl@3/bin/c_rehash openssl@3 is keg-only, which means it was not symlinked into /opt/homebrew, because macOS provides LibreSSL. If you need to have openssl@3 first in your PATH, run: echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc For compilers to find openssl@3 you may need to set: export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib" export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include" For pkg-config to find openssl@3 you may need to set: export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@3/lib/pkgconfig" ==> Summary 🍺 /opt/homebrew/Cellar/openssl@3/3.0.2: 6,429 files, 28.0MB ==> Running `brew cleanup openssl@3`... Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP. Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`). ==> Caveats ==> openssl@3 A CA file has been bootstrapped using certificates from the system keychain. To add additional certificates, place .pem files in /opt/homebrew/etc/openssl@3/certs and run /opt/homebrew/opt/openssl@3/bin/c_rehash openssl@3 is keg-only, which means it was not symlinked into /opt/homebrew, because macOS provides LibreSSL. If you need to have openssl@3 first in your PATH, run: echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc For compilers to find openssl@3 you may need to set: export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib" export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include" For pkg-config to find openssl@3 you may need to set: export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@3/lib/pkgconfig" ``` 安装完成之后然后再ls一下,发现目录下 openssl@3 在目录下面了 ``` admin@admindeMacBook-Air opt % ls ca-certificates openssl openssl@3 pkg-config pkgconfig ``` 这样的话,环境变量OPENSSL_LIB_DIR、OPENSSL_INCLUDE_DIR指向的目录也都存在了, 然后再cargo run 一下,发现程序正常了,问题解决!

fltk-rs踩坑记录,解决could not find native static library cfltk, perhaps an -L flag is missing 问题

2022-03-18 20:52:15
123

# fltk-rs踩坑记录 > 最近想用rust写一个简单的gui程序,所以查了一下rust的gui库,好像rust在gui方面发展还不好,但是我要实现的很简单,所以最终选择了fltk-rs来开发。 现在只是为了跑通helloworld,所以只碰到这一个问题,以后在碰到别的会再补充。 ## 项目地址: https://github.com/fltk-rs/fltk-rs ## 按说明安装 按照作者的说明,依赖添加为 ``` [dependencies] fltk = "^1.3" ``` 然后把helloworld的代码写好后,执行cargo run。 但是编译的时候出现问题,总是报错 ``` Compiling fltk-sys v1.0.17 Compiling fltk v1.0.18 error: could not find native static library cfltk, perhaps an -L flag is missing? error: aborting due to previous error error: could not compile fltk-sys ``` ## 找寻解决方案 于是我在作者的github的issue里面找到了一个类似的,https://github.com/fltk-rs/fltk-rs/issues/723, 但是得到的答案是把依赖改为 ``` [dependencies] fltk = { version = "1", features = ["fltk-bundled"] } ``` 还有一个也比较类似: https://github.com/fltk-rs/fltk-rs/discussions/1116 最终在这里找到了答案: https://fltk-rs.github.io/fltk-book/Setup.html#detailed-setup 原来运行fltk-rs需要在电脑上安装有cmake和git,我电脑上`没有安装cmake`,所以有这个报错信息。 所以我的目的就是安装cmake ## 检测是否安装了cmake 在命令行中输入 cmake --version 如果返回了版本信息,就说明安装成功了,否则就是未安装成功 ``` mac安装地址: https://blog.csdn.net/xujiuba/article/details/107234040 windows安装教程: https://blog.csdn.net/u011231598/article/details/80338941 ``` 按照教程安装完成之后,要重新打开一个shell(在安装cmake之前的shell界面没法使用到cmake命令),然后输入`cmake --version`,能看到版本信息即为安装成功 ## 最后再编译 保证 `git --version` 和 `cmake --version` 能够获取到版本信息,则可以再次编译项目了 这次应该就可以成功编译了。 ## 其他问题 - 如果是windows的话,build之后的exe文件运行的时候,后面总有一个黑色的命令行界面,如何去除? 在main.rs 的最前面加上 `#![windows_subsystem = "windows"]` ,然后再编译即可

搜集的一些中文版的rust语言学习资料

2022-03-04 11:27:14
205

> 记录一些rust学习文档,很多rust文档都是英文版的,阅读的话效率很慢,这里搜集一些中文版本的文档,有一些是翻译的中文版本,希望对你有用~ - rust烹饪书 https://llever.com/rust-cookbook-zh/ - rust编程语言 https://learnku.com/docs/rust-lang/2018/about-this-book/4591 - rust死灵书 https://www.bookstack.cn/read/rustonomicon_zh-CN/src-0.%e7%ae%80%e4%bb%8b.md - rust开源杂志 https://rustmagazine.github.io/rust_magazine_2021/ - rust 程序设计语言 https://kaisery.github.io/trpl-zh-cn/title-page.html - rust语言圣经 https://course.rs/ - 绅士的介绍rust http://llever.com/gentle-intro/readme.zh.html - 通过例子学rust https://rustwiki.org/zh-CN/rust-by-example/index.html - rust标准库中文版 https://rustwiki.org/zh-CN/std/ - rust基础教程 https://www.twle.cn/c/yufei/rust/rust-basic-index.html - rust学习笔记 https://skyao.io/learning-rust/docs.html - RustPrimer中文版 https://rustcc.gitbooks.io/rustprimer/content/ - rust 蝴蝶教程 https://www.jc2182.com/rust/rust-jiaocheng.html

rust使用vec在遍历时删除元素

2022-02-24 18:09:01
75

rust使用vec在遍历时删除元素 需求: 工作中有一个地方需要用到在遍历时把不符合条件的元素删除掉, > 比如一个vec中是原始数据是 `vec![1,2,3,3,4,5]` ,然后我要在遍历中把等于c的元素删除掉,目的是得到`vec![1,2,4,5]` ## 第一次错误尝试 由于最开始只知道移除元素用remove方法,所以最开始是这样写的 ``` rust let mut items:Vec<&str> = vec!["a", "b", "c", "c", "d", "e"]; println!("before items is {:?}", items); for (index, item) in items.iter().enumerate() { if *item == "c" { items.remove(index); } } println!("then items is {:?}", items); ``` 但是报错了,报错提示为 ``` error[E0502]: cannot borrow `items` as mutable because it is also borrowed as immutable --> src/main.rs:8:13 | 6 | for (index, item) in items.iter().enumerate() { | ------------------------ | | | immutable borrow occurs here | immutable borrow later used here 7 | if *item == "c" { 8 | items.remove(index); | ^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here For more information about this error, try `rustc --explain E0502`. ``` 提示很明显,iter()是不可变的引用,但是调用remove的时候删除一个元素得时候,对items是可变的引用了,所以一个变量不能既是可变引用又是不可变引用,所以报错了。 ## 第二次错误尝试 然后我进行了第二次尝试, 代码改成了,不在遍历中进行移除,把需要移除的id保存起来,在遍历结束之后,再把需要移除的元素给移除掉。 ``` rust let mut items:Vec<&str> = vec!["a", "b", "c", "c", "d", "e"]; println!("before items is {:?}", items); let mut remove_indexs: Vec<usize> = Vec::new(); for (index, item) in items.iter().enumerate() { if *item == "c" { remove_indexs.push(index); } } println!("remove indexs is {:?}", remove_indexs); for i in remove_indexs { items.remove(i); } println!("then items is {:?}", items); ``` 打印出的结果是 ``` before items is ["a", "b", "c", "c", "d", "e"] remove indexs is [2, 3] then items is ["a", "b", "c", "e"] ``` 不报错了,但是结果不对啊,预计是[“a”,"b","d","e"]的,怎么变成了["a", "b", "c", "e"]了。 原因是: 第二个循环需要移除`索引为2和3`的两个元素。所以有两次循环: - 第一次:在移除`索引为2`的元素之后。items变成了[“a”,"b","c","d","e"],后面的元素补上去了。这时我们的`目标索引3`的index从3变成了2. - 第二次:按照代码把`索引为3`的元素移除,但是索引3的元素却成c变成了d,这段程序也就错了。 ## 正确方法 上面方法失败之后,觉得vec应该有处理这种情况的方法,所以看了源码,找到了答案。 vec有两个方法可以实现我想要的。 vec.retain 和 vec.drain_filter ### vec.retain vec.retain 很简单,`retain`的意思是`保留`,所以这个方法的意思就是接收一个回调函数,然后回调函数里面返回true进行保留,返回false的就移除。 示例: ``` rust let mut vec = vec![1, 2, 3, 4]; vec.retain(|&x| x % 2 == 0); assert_eq!(vec, [2, 4]); ``` 所以用vec.retain来实现的话,就是这样 ``` rust let mut items:Vec<&str> = vec!["a", "b", "c", "c", "d", "e"]; println!("before items is {:?}", items); items.retain(|item| if *item == "c" {false } else { true }); println!("then items is {:?}", items); ``` ### vec.drain_filter `drain`的意思是 `排出` 的意思,所以这个函数就是排出过滤器,接收一个回调函数,然后把回调函数里面返回true的元素就会排出,自然也就从原本的vec里面删除掉了。然后有需要的话还可以搜集排出的元素。 示例: ``` rust let mut numbers = vec![1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15]; let evens = numbers.drain_filter(|x| *x % 2 == 0).collect::<Vec<_>>(); let odds = numbers; assert_eq!(evens, vec![2, 4, 6, 8, 14]); assert_eq!(odds, vec![1, 3, 5, 9, 11, 13, 15]); ``` 所以用vec.drain_filter来实现的话,就是这样 ``` rust let mut items:Vec<&str> = vec!["a", "b", "c", "c", "d", "e"]; println!("before items is {:?}", items); items.retain(|item| if *item == "c" {false } else { true }); let removed_items = items.drain_filter(|item| if *item == "c" { true} else { false}).collect::<Vec<_>>(); println!("then items is {:?}", items); println!("removed item is {:?}", removed_items); ``` 但是报错了,提示: ``` error[E0658]: use of unstable library feature 'drain_filter': recently added --> src/main.rs:7:31 | 7 | let removed_items = items.drain_filter(|item| if *item == "c" { true} else { false}).collect::<Vec<_>>(); | ^^^^^^^^^^^^ | = note: see issue #43244 <https://github.com/rust-lang/rust/issues/43244> for more information For more information about this error, try `rustc --explain E0658`. ``` 这个函数是属于不稳定的特性的,所以需要使用的话是有条件的 使用unstable feature的条件和步骤: 1. 只有nightly才可以使用unstable 2. 找到unstable feature的名字 3. #![feature(xxx)]启用这个feature 感兴趣的可以参考:https://blog.csdn.net/varding/article/details/48206689 来启用

rust连接oracle数据库遇到DPI-1047: Cannot locate a 64-bit Oracle Client library的解决方案

2022-02-18 19:56:19
134

这两天要实现一个用rust连接远程的oracle数据库的需求,所以就需要用rust连接oracle。 在github上面找到一个库,地址:https://github.com/kubo/rust-oracle 直接使用时,发现报错,打印报错信息: > Err(DpiError(DbError { code: 0, offset: 3416999480, message: "DPI-1047: Cannot locate a 64-bit Oracle Client library: \"libclntsh.so: cannot open shared object file: No such file or directory\". See https://oracle.github.io/odpi/doc/installation.html#linux for help", fn_name: "dpiContext_createWithParams", action: "load library" })) 根据 https://oracle.github.io/odpi/doc/installation.html#linux 上的说明,大概查了一下,大概明白了。 这个rust-oracle不是直接连接远程oracle的,而是需要在服务器上加载一个oracle客户端的库,然后调用这个库才可以连接到oracle上。 - oracle客户端下载地址:https://www.oracle.com/database/technologies/instant-client/downloads.html 本来是在macos m1上面开发的,但是m1是arm架构的,而官网只有x64架构的,所以就只能使用虚拟机上的ubuntu了。 下面是操作步骤: ``` 1. sudo mkdir -p /opt/oracle #创建一个目录来保存oracle客户端文件 2. cd /opt/oracle #进入上面创建的这个目录 3. wget https://download.oracle.com/otn_software/linux/instantclient/191000/instantclient-basic-linux.arm64-19.10.0.0.0dbru.zip #下载客户端文件,我下载的是zip格式的,等下再解压 4. unzip instantclient-basic-linux.arm64-19.10.0.0.0dbru.zip #解压下载好的文件到当前目录 解压好之后,ls看一下,可以看到当前目录多了一个目录instantclient_19_10 这个目录里面的文件就是需要的.so库文件,剩下的任务就是要在rust里面能够加载到这个库. //意思是把/opt/oracle/instantclient_19_10写入到/etc/ld.so.conf.d/oracle-instantclient.conf文件,文件不存在则新建 5. sudo sh -c "echo /opt/oracle/instantclient_19_10 > /etc/ld.so.conf.d/oracle-instantclient.conf" //用ldconfig 执行文件将 /etc/ld.so.conf(也就是/etc/ld.so.conf.d/文件夹下面的所有conf文件)的数据读入高速缓存中,同时记录到/etc/ld.so.cache文件中。 6.ldconfig ``` 这样的话,linux系统就能把客户端库加载到,然后rust-oracle就能连接到oracle了。 参考资料: - https://oracle.github.io/odpi/doc/installation.html#oracle-instant-client-zip-files - https://man7.org/linux/man-pages/man8/ld.so.8.html

rust把json字符串转为结构体的一些经验

2022-02-17 12:16:21
56

> 在rust里面处理json字符串时,一个常用的库是serde_json,网上也有很多资料,这里就不再赘述了。如 - https://www.qttc.net/509-rust-parse-json.html - https://docs.rs/serde_json/latest/serde_json/ 这里分享一下我的一点经验: 这里是常规的把字符串转为结构体的代码 ``` rust #[derive(Debug,Deserialize)] struct ConfigStruct { username: String, password: String } pub async fn demo() { let config_str = r#"{"username":"hello","password":"world"}"#; let config: ConfigStruct = serde_json::from_str(config_str).unwrap(); println!("config is {:?}", config); } ``` 这里config_str是固定的正确的,但是我的需求里面,是需要前端传来我想要结构的json字符串,但是如果前端传来的不是我想要的结构的呢,那直接调用unwrap的话,就会直接panic,导致程序挂掉。 所以我想要的是,在后端接收前端传来的json字符串,然后尝试转为想要的结构体来验证参数是否正确。 **如果参数正确,那么逻辑继续,如果参数错误,就要返回提示,而不是直接在程序里面panic** 修改后的代码是 ``` rust #[derive(Debug,Deserialize)] struct ConfigStruct { username: String, password: String } pub async fn demo() { //暂定下面两个字符串是前端传来的 let config_str = r#"{"user":"hello","pwd":"world"}"#; //错误参数 // let config_str = r#"{"username":"hello","password":"world"}"#;//正确参数 let config_res= serde_json::from_str(config_str).map(|c:ConfigStruct| c); if config_res.is_err() { println!("传来的参数错误,错误信息是{:?}", config_res.unwrap_err().to_string()); }else { println!("传来的参数正确"); println!("config is {:?}", config_res.unwrap()); } } ```

warp框架相关笔记

2022-02-15 16:35:34
46

> warp框架的路由比较独特,是由过滤器来组成的,这里记录一些常用的路由,免得遗忘。 如果路由没有匹配到, 则返回 http状态码 405 HTTP method not allowed ### /hello/{id} 路由 假如想要 /hello/{id} 这样的一个路由,其中hello是固定的,id是固定的无符号值 比如/hello/1, /hello/123 这样 可以用这两种方法来写 ``` rust //使用path宏 let one = warp::path!("hello" / u32).map(|num| format!("你输入的数字是{}", num)); //使用path方法 let two = warp::path("hello").and(warp::path::param()).map(|num:u32| { format!("你输入的数字是{}", num) }); ``` 我的理解是, path宏的话,很直观的表达出来,hello/后面跟u32的值,然后匹配到map里面,因为在路由里面已经指定了hello/后面的是u32的数字,所以map的闭包里面就不需要为num指定类型了。 path方法的话,用and连接来捕获一个参数,然后去和map中指定的类型进行对比,如果获取到的参数是u32的话,就能走到map里面了。 如果传入的参数不是u32的,还是会返回405状态码。

rust学习中遇到的干货文章

2022-02-14 19:43:33
24

Rust异步浅谈 https://leaxoy.github.io/2020/03/rust-async-runtime/ 如何在 Rust 异步应用程序正确使用锁 https://jishuin.proginn.com/p/763bfbd6b0df 学习异步运行时tokio https://www.zhihu.com/column/c_1469317336243478528 英文但是不错的rust学习文档 https://learning-rust.github.io/docs/index.html https://cheats.rs/

rust国内镜像配置

2022-02-11 13:02:39
29

## rustup镜像配置 rustup 设置环境变量 RUSTUP_DIST_SERVER(用于更新 toolchain) ``` export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static ``` 以及 RUSTUP_UPDATE_ROOT(用于更新 rustup) ``` export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup ``` Windows 下对应的设置环境变量的 PowerShell 命令为: ``` $env:RUSTUP_DIST_SERVER="https://mirrors.ustc.edu.cn/rust-static" $env:RUSTUP_UPDATE_ROOT="https://mirrors.ustc.edu.cn/rust-static/rustup" ``` ## create.io镜像配置 在 $HOME/.cargo/config 中添加如下内容 ``` [registry] index = "git://mirrors.ustc.edu.cn/crates.io-index" ``` 如果所处的环境中不允许使用 git 协议, 可以把上述地址改为 ``` index = "http://mirrors.ustc.edu.cn/crates.io-index" ``` 同步频率为每两个小时更新一次. 如果 cargo 版本为 0.13.0 或以上, 需要更改 $HOME/.cargo/config 为以下内容: ``` [source.crates-io] registry = "https://github.com/rust-lang/crates.io-index" replace-with = 'ustc' [source.ustc] registry = "git://mirrors.ustc.edu.cn/crates.io-index" ``` 原文地址: https://mirrors.ustc.edu.cn/help/rust-static.html https://mirrors.ustc.edu.cn/help/crates.io-index.html

列表为空