-
RUST연습]Rust Crash Course | Rustlang -44:47부터코딩Coding/Rust연습 2021. 12. 7. 21:42728x90
main.rs
// mod print; // mod vars; // mod types; mod strings; fn main() { // print::run(); // vars::run(); // types::run(); strings::run(); }
strings.rs
// Primitive str = Immutable fixed-length string somewhere in memory // String = Growable, heap-allocated data structure - Use when you need modify or own string data pub fn run() { let mut hello = String::from("Hello "); //Get length println!("Length: {}", hello.len()); // Push char hello.push('W'); // Push string hello.push_str("orld!"); // Capacity in bytes println!("Capactity: {}", hello.capacity()); // Check if empty println!("Is Empty: {}", hello.is_empty()); // Contains println!("Contains 'World' {}", hello.contains("World")); // Replace println!("Replace: {}", hello.replace("World", "There")); // Loop through string by whitespace for word in hello.split_whitespace() { println!("{}", word); } // Create stirng with capacity let mut s = String::with_capacity(10); s.push('a'); s.push('b'); //Assertion testing assert_eq!(2, s.len()); assert_eq!(10, s.capacity()); println!("{}", s); }
main.rs
// mod print; // mod vars; mod types; fn main() { // print::run(); // vars::run(); types::run(); }
types.rs
/* Primitive Type Intergers: u8, i8, u16, i16, u32, i32, u64, i64, u128, i128(number of bits they take in memory) Floats: f32, f64 Boolean (bool) Characters(char) Tuples Arrays */ // Rust is a statically typed language, which means that it must know the types of all variable at compile time, however, the compiler can usually infer what type we want to use based on the value and how we use it. pub fn run() { // Default is "i32" let x = 1; // Default is "f64" let y = 2.5; // Add explicit type let z: i64 = 454545455; // Find max size println!("Max i32: {}", std::i32::MAX); println!("Max i64: {}", std::i64::MAX); // Boolean let is_active: bool = true; // Get boolean from expression let is_greater: bool = 10 < 5; let a1 = 'a'; let face = '\u{1F600}'; println!("{:?}", (x, y, z, is_active, is_greater, a1, face)); }
https://www.youtube.com/watch?v=zF34dRivLOw
반응형'코딩Coding > Rust연습' 카테고리의 다른 글
[Rust연습]Easy Rust 024: Control flow 1(if, else if, else) (0) 2021.12.12 RUST연습]Easy Rust 022: Vecs (0) 2021.12.12 RUST연습]변수 사이즈크기 알아보기(use std::mem::size_of;) (0) 2021.12.10 Rust연습]Shadowing (0) 2021.12.09 Rust☆]Understanding ☆Ownership☆ in Rust&☆The Rules of References☆ (0) 2021.12.06 Rust연습]Control Flow_While문_3.2.1.LIFTOFF!!연습 (0) 2021.12.06 Rust연습]Common Programming Concepts in Rust (0) 2021.12.06 Rust연습) Guessing Game in Rust (0) 2021.12.04