随笔分类 - 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
阅读全文
摘要: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
阅读全文
摘要:Define a macro and use it: macro_rules! my_macro { () => { println!("Check out my macro!"); }; } fn main() { my_macro!(); } Notice that you have time
阅读全文
摘要:Methods: mod sausage_factory { // private method fn get_secret_recipe() -> String { String::from("Ginger") } // public method pub fn make_sausage() {
阅读全文
摘要: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
阅读全文
摘要:https://doc.rust-lang.org/std/string/struct.String.html#method.trim fn string_slice(arg: &str) { println!("{}", arg); } fn string(arg: String) { print
阅读全文
摘要: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
阅读全文
摘要:https://doc.rust-lang.org/stable/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax Similar to Javas
阅读全文
摘要: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
阅读全文
摘要:https://doc.rust-lang.org/book/ch05-01-defining-structs.html struct ColorClassicStruct { red: i32, green: i32, blue: i32 } struct ColorTupleStruct(i32
阅读全文
摘要:fn main() { let cat = ("Furry McFurson", 3.5); let (name, age) = cat; println!("{} is {} years old.", name, age); } Example 2: #[test] fn indexing_tup
阅读全文
摘要: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.");
阅读全文
摘要:struct Rectangle { width: i32, height: i32 } impl Rectangle { // Only change the test functions themselves pub fn new(width: i32, height: i32) -> Self
阅读全文
摘要:pub fn is_even(num: i32) -> bool { num % 2 == 0 } /* This attribute indicates that the following module is a conditional compilation module that shoul
阅读全文
摘要: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
阅读全文
摘要:Constants variable always need to be annotated: const NUMBER: i32 = 3; fn main() { println!("Number {}", NUMBER); }
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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(
阅读全文
摘要: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
阅读全文