开始记录

学习、生活、感悟

  • 🌟 学而不思则罔,思而不学则殆
  • 🌟 实事求是
  • 🌟 博观而约取,厚积而薄发

rust学习笔记-6

跟着教程手敲一个minigrep程序。敲完之后对之前的学习有了个大体的映像。需要回去复习一下enum。Result<Config, &'static str>这种初次敲起了还是有点蒙圈。 src/lib.rs use std::error::Error; use std::fs; use std::env; pub struct Config{ query: String, file_path: String, ignore_case: bool, } impl Config { pub fn build(args: &[String]) -> Result<Config, &'static str> { if args.len() < 3 { return Err("not enough arguments"); } let query = args[1].clone(); let file_path = args[2].clone(); let ignore_case = env::var("IGNORE_CASE").is_ok(); return Ok(Config {query, file_path, ignore_case}) } } pub fn run(config: Config) -> Result<(), Box<dyn Error>>{ let contents = fs::read_to_string(config....

June 25, 2024 · akawhy

rust中变量自动推断问题

在阅读《An I/O Project: Building a Command Line Program》 这一章的时候遇到了个问题。文章里给出了这样一个例子: use std::env; fn main() { let args: Vec<String> = env::args().collect(); //let args = env::args().collect(); dbg!(args); } 高亮的那行是当时手敲代码,省略了类型。运行cargo build发现编译不通过,报如下错误: error[E0283]: type annotations needed --> src/main.rs:5:9 | 5 | let args = env::args().collect(); | ^^^^ ------- type must be known at this point | = note: cannot satisfy `_: FromIterator<String>` note: required by a bound in `collect` --> /Users/XXXXXXXX/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:1999:19 | 1999 | fn collect<B: FromIterator<Self::Item>>(self) -> B | ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::collect` help: consider giving `args` an explicit type | 5 | let args: Vec<_> = env::args()....

June 21, 2024 · akawhy

rust学习笔记-5

泛型 使用<T>来表示使用泛型,这块和golang相差不大 struct Point<X1, Y1> { x: X1, y: Y1, } impl<X1, Y1> Point<X1, Y1> { fn mixup<X2, Y2>(self, other: Point<X2, Y2>) -> Point<X1, Y2> { Point { x: self.x, y: other.y, } } } fn main() { let p1 = Point { x: 5, y: 10.4 }; let p2 = Point { x: "Hello", y: 'c' }; let p3 = p1.mixup(p2); println!("p3.x = {}, p3.y = {}", p3.x, p3.y); } traits 特征 这个类似其他语言中的接口(interface)。比较特别的地方是可以在这个特征中实现具体的函数,简化这个特征的实现。...

June 19, 2024 · akawhy

rust学习笔记-4

模块 说实话,教程这块看得有点晕: Start from the crate root: When compiling a crate, the compiler first looks in the crate root file (usually src/lib.rs for a library crate or src/main.rs for a binary crate) for code to compile. Declaring modules: In the crate root file, you can declare new modules; say, you declare a “garden” module with mod garden;. The compiler will look for the module’s code in these places: Inline, within curly brackets that replace the semicolon following mod garden In the file src/garden....

June 11, 2024 · akawhy

rust学习笔记-3

ownership & borrow 这里看起来会稍微有点复杂,核心是看rust如何通过编译约束来保证内存使用安全 基础逻辑: Each value in Rust has an owner. There can only be one owner at a time. When the owner goes out of scope, the value will be dropped. 变量赋值会产生owner的转移 let s1 = String::from("hello"); let s2 = s1; // 产生了转移,s1使用会报错 println!("{}, world!", s1); 报错信息: $ cargo run Compiling ownership v0.1.0 (file:///projects/ownership) error[E0382]: borrow of moved value: `s1` --> src/main.rs:5:28 | 2 | let s1 = String::from("hello"); | -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait 3 | let s2 = s1; | -- value moved here 4 | 5 | println!...

June 9, 2024 · akawhy