-
Rust연습] tuple연습❤️ & ndarray ❤️연습코딩Coding/Rust연습 2022. 6. 2. 00:42728x90
Cargo.tomldependencies] ndarray = "0.15.4"
main.rs// https://doc.rust-lang.org/rust-by-example/primitives/tuples.html // Tuples can be used as function arguments and as return values // https://rust-lang-nursery.github.io/rust-cookbook/science/mathematics/linear_algebra.html use ndarray::arr2; fn reverse(pair: (i32, bool)) -> (bool, i32) { // `let` can be used to bind the members of a tuple to variables let (integer, boolean) = pair; (boolean, integer) } fn main() { let my_tuple = ("hello", 3, 'a', 2.55, "good"); println!("{} {} {}", my_tuple.0, my_tuple.2, my_tuple.3) let long_tuple = ( 1u8, 2u16, 3u32, 4u64, -1i8, -2i16, -3i32, -4i64, 0.1f32, 0.2f64, 'a', true, ); println!("long tuple first value: {}", long_tuple.0); println!("long tuple second value: {}", long_tuple.1); let tuple_of_tuples = ((1u8, 2u16, 2u32), (4u64, -1i8), -2i16); println!("tuple of tuples: {:?}", tuple_of_tuples); // let too_long_tuple = ( // 1i32, 2i32, 3i32, 4i32, 5i32, 6i32, 7i32, 8i32, 9i32, 10i32, 11i32, 12i32, 13i32, // ); // println!("too long tuple: {:?}", too_long_tuple); let pair = (1, true); println!("pair is {:?}", pair); println!("the reversed pair is {:?}", reverse(pair)); // To create one element tuples, the comma is required to tell them apart // from a literal surrounded by parenthese println!("one element tuple: {:?}", (5u32,)); println!("just an integer: {:?}", (5u32)); // tuples can be destuctured to create bindings let tuple = (1, "hello", 4.5, true); let (a, b, c, d) = tuple; println!("{:?}, {:?}, {:?}, {:?}, ", a, b, c, d); let ndarray_a = arr2(&[[1, 2, 3], [4, 5, 6]]); let ndarray_b = arr2(&[[6, 5, 4], [3, 2, 1]]); let sum = &ndarray_a + &ndarray_b; println!("{}", ndarray_a); println!("+"); println!("{}", ndarray_b); println!("="); println!("{}", sum); }
result:hello a 2.55 long tuple first value: 1 long tuple second value: 2 tuple of tuples: ((1, 2, 2), (4, -1), -2) pair is (1, true) the reversed pair is (true, 1) one element tuple: (5,) just an integer: 5 1, hello, 4.5, true, [[1, 2, 3], [4, 5, 6]] + [[6, 5, 4], [3, 2, 1]] = [[7, 7, 7], [7, 7, 7]]
출처:
https://rust-lang-nursery.github.io/rust-cookbook/science/mathematics/linear_algebra.html
https://doc.rust-lang.org/rust-by-example/primitives/tuples.html
러스크의 튜플은 12개가 한계
Why is❤️tuple formatting limited to 12 items in Rust? - https://economiceco.tistory.com/m/13956
반응형'코딩Coding > Rust연습' 카테고리의 다른 글
Rust연습❤️내 컴퓨터에 병렬 실행 가능한 코어수 알아보기available_parallelism (0) 2022.06.29 Rust – Fibonacci using Recursion and Iteration (0) 2022.06.26 Rust연습❤️) Collect & Vec 조합 Closure활용 (0) 2022.06.23 Rust연습)그래프 그리기 + 움직이는 별 표현하기(움직이는 gif만들기) (0) 2022.06.15 Rust연습] Cow__120 Easy Rust in Korean: to_mut with Cow (0) 2022.04.19 Rust연습]Cow part2 (0) 2022.04.19 Rust연습] cow_빨라서 최고borrow에서 적극 활용하자 (0) 2022.04.19 rust연습]vec벡터 끼리 합치기 + vec + vec append (0) 2022.04.18