Search
 
SCRIPT & CODE EXAMPLE
 

RUST

rust named tuple

/* A named tuple is its own new type, just with anonymous
 * fields like a regular tuple.
 * This is in contrast to type aliases that are simply
 * new names for the same type.
 */

// Named Tuple ==========================================
struct Vec2D(f32, f32);
struct Pos2D(f32, f32);
fn take_vec2d(vec: Vec2D) { }

// Type Alias ===========================================
type Vec3D = (f32, f32, f32);
type Pos3D = (f32, f32, f32);
fn take_vec3d(vec: Vec3D) { }

// ======================================================
fn main() {
    let vec2d = Vec2D(1., 1.);
    let pos2d = Pos2D(vec2d.0, vec2d.1);

    take_vec2d(vec2d); // works
    // take_vec2d(pos2d); // panics

    let vec3d: Vec3D = (1., 1., 1.);
    let pos3d: Pos3D = (vec3d.0, vec3d.1, vec3d.2);

    take_vec3d(vec3d); // works
    take_vec3d(pos3d); // works
}
Comment

PREVIOUS NEXT
Code Example
Rust :: armanriazi•rust•error•[E0369]: binary operation `=` cannot be applied to type `T` 
Rust :: slice indices are of type usize rust 
Rust :: rust `cfg` which is always true / false 
Rust :: rust•armanriazi•unwrap 
Rust :: armanriazi•rust•concept•oop•state•pattern 
Rust :: rust•armanriazi•static•str 
Rust :: rust lang unresolved import 
Rust :: minimum and maximum numbers for various integer types 
Rust :: armanriazi•rust•trait•external•implement•coherence•orphan 
Rust :: blank struct rust 
Rust :: rust error: failed to run custom build command for python3-sys 
Rust :: rust range step 
Lua :: luau rainbow part 
Lua :: how to comment multiple lines in lua 
Lua :: luau how to make a kill brick 
Lua :: roblox tween 
Lua :: color3 not working lua 
Lua :: how do you close the lua program 
Lua :: repeat until lua 
Lua :: roblox check if in private server 
Lua :: lua script 
Lua :: how to make everyone on team see name roblox 
Lua :: run a function in lua 
Lua :: lua remove duplicates from table 
Lua :: lua class example 
Matlab :: matlab symbolic function 
Matlab :: octave clear figure 
Basic :: git access token 
Basic :: Loop inner fiter() 
Elixir :: phoenix query get first record 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =