随笔分类 -  Rust

摘要:Following code has compile error: #[test] fn main() { let vec0 = vec![22, 44, 66]; let mut vec1 = fill_vec(vec0); assert_eq!(vec1, vec![22, 44, 66, 88 阅读全文
posted @ 2024-02-27 15:37 Zhentiw 阅读(6) 评论(0) 推荐(0) 编辑
摘要:Following code has borrow problem: #[test] fn main() { let vec0 = vec![22, 44, 66]; let vec1 = fill_vec(vec0); assert_eq!(vec0, vec![22, 44, 66]); ass 阅读全文
posted @ 2024-02-27 15:33 Zhentiw 阅读(8) 评论(0) 推荐(0) 编辑
摘要:Define a macro and use it: macro_rules! my_macro { () => { println!("Check out my macro!"); }; } fn main() { my_macro!(); } Notice that you have time 阅读全文
posted @ 2024-02-27 15:23 Zhentiw 阅读(5) 评论(0) 推荐(0) 编辑
摘要:Methods: mod sausage_factory { // private method fn get_secret_recipe() -> String { String::from("Ginger") } // public method pub fn make_sausage() { 阅读全文
posted @ 2024-02-27 14:51 Zhentiw 阅读(6) 评论(0) 推荐(0) 编辑
摘要:The TryFrom and try_into() methods are part of the. standard libaray's conversion traits, designed to handle conversions between types in a fallible m 阅读全文
posted @ 2024-02-26 14:54 Zhentiw 阅读(11) 评论(0) 推荐(0) 编辑
摘要:https://doc.rust-lang.org/std/string/struct.String.html#method.trim fn string_slice(arg: &str) { println!("{}", arg); } fn string(arg: String) { print 阅读全文
posted @ 2024-02-26 03:15 Zhentiw 阅读(8) 评论(0) 推荐(0) 编辑
摘要:Structs contain data, but can also have logic. In this exercise we have defined the Package struct and we want to test some logic attached to it. #[de 阅读全文
posted @ 2024-02-26 02:51 Zhentiw 阅读(7) 评论(0) 推荐(0) 编辑
摘要:https://doc.rust-lang.org/stable/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax Similar to Javas 阅读全文
posted @ 2024-02-26 02:44 Zhentiw 阅读(10) 评论(0) 推荐(0) 编辑
摘要:fn multiply(x: i64, y: u8) -> i64 { return x * (y as i64); } Here we convert u8to i64, which is possible since i64has a wider range than u8; but you c 阅读全文
posted @ 2024-02-24 19:01 Zhentiw 阅读(2) 评论(0) 推荐(0) 编辑
摘要:https://doc.rust-lang.org/book/ch05-01-defining-structs.html struct ColorClassicStruct { red: i32, green: i32, blue: i32 } struct ColorTupleStruct(i32 阅读全文
posted @ 2024-02-23 19:11 Zhentiw 阅读(9) 评论(0) 推荐(0) 编辑
摘要:fn main() { let cat = ("Furry McFurson", 3.5); let (name, age) = cat; println!("{} is {} years old.", name, age); } Example 2: #[test] fn indexing_tup 阅读全文
posted @ 2024-02-23 19:04 Zhentiw 阅读(3) 评论(0) 推荐(0) 编辑
摘要:fn main() { let a = 0..100; if a.len() >= 100 { println!("Wow, that's a big array!"); } else { println!("Meh, I eat arrays like that for breakfast."); 阅读全文
posted @ 2024-02-23 18:57 Zhentiw 阅读(8) 评论(0) 推荐(0) 编辑
摘要:struct Rectangle { width: i32, height: i32 } impl Rectangle { // Only change the test functions themselves pub fn new(width: i32, height: i32) -> Self 阅读全文
posted @ 2024-02-23 18:50 Zhentiw 阅读(22) 评论(0) 推荐(0) 编辑
摘要:pub fn is_even(num: i32) -> bool { num % 2 == 0 } /* This attribute indicates that the following module is a conditional compilation module that shoul 阅读全文
posted @ 2024-02-23 18:46 Zhentiw 阅读(14) 评论(0) 推荐(0) 编辑
摘要:Code has error: fn main() { let answer = square(3); println!("The square of 3 is {}", answer); } fn square(num: i32) -> i32 { num * num; } Error: ⚠️ C 阅读全文
posted @ 2024-02-23 15:39 Zhentiw 阅读(8) 评论(0) 推荐(0) 编辑
摘要:Constants variable always need to be annotated: const NUMBER: i32 = 3; fn main() { println!("Number {}", NUMBER); } 阅读全文
posted @ 2024-02-23 15:02 Zhentiw 阅读(3) 评论(0) 推荐(0) 编辑
摘要:Ref to : https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing fn main() { let number = "T-H-R-E-E"; // don't change this lin 阅读全文
posted @ 2024-02-23 14:59 Zhentiw 阅读(11) 评论(0) 推荐(0) 编辑
摘要:This lesson shows how to use a Rust loop to run a program infinitely. use std::io; use std::process; fn main() { loop { println!("Please enter a first 阅读全文
posted @ 2024-02-23 14:39 Zhentiw 阅读(4) 评论(0) 推荐(0) 编辑
摘要:In this lesson we'll learn how to exit a program using the std::process module in Rust and it's exit() method. use std::io; use std::process; fn main( 阅读全文
posted @ 2024-02-23 14:38 Zhentiw 阅读(13) 评论(0) 推荐(0) 编辑
摘要:In this lesson we'll explore how to unwrap a Result type using a language feature called Pattern Matching. use std::io; fn main() { let mut first = St 阅读全文
posted @ 2024-02-23 14:33 Zhentiw 阅读(3) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示