d语言对比rust

d在简洁性和表达性方面,胜了很多.而rust明确性更好.rust更学术,D实用.

module main;
import std.stdio;
void main()
{
    writeln( "Hello, 世界" );
}

fn main() {
    println!("Hello, 世界")
}

有隐式导入.可省略分号,它用于转换表达式为语句.

module main;

import std.stdio;
import std.random;

void main()
{
    writeln( "最喜欢数为", uniform( 0 , 10 ) );
}

extern crate rand;

use rand::distributions::{IndependentSample, Range};

fn main() {
    let between = Range::new(0, 10);
    let mut rng = rand::thread_rng();
    println!("最喜欢数为{}", between.ind_sample(&mut rng));
}

很谨慎的扩展标准库.要依赖第3方包.

导入

module main;

import
    std.stdio,
    std.math;

void main()
{
    import std.conv;
}

use std::{path, env};

fn main() {
    use std::convert;
}

允许分组,但不能用相对路径:

导入必须在最前面.

D默认公开,Rust默认私有.带pub才导出.

mod test {
    pub struct PublicStruct {
        pub a: i32,
    }

    pub struct NoSoPublicStruct {
        pub a: i32,
        b: i32,
    }

    struct PrivateStruct {
        a: i32,
    }

    pub struct PublicTupleStruct(pub i32, pub i32);
    pub struct TupleStruct(pub i32, i32);
    struct PrivateTupleStruct(i32, i32, i32);

    pub fn create() -> NoSoPublicStruct {
        NoSoPublicStruct { a: 10, b: 20 }
    }

    fn create_private() -> PublicTupleStruct {
        PublicTupleStruct(1, 2)
    }
}

use test::{PublicStruct, NoSoPublicStruct, PublicTupleStruct, create};
fn main() {
    let _a = PublicStruct { a: 10 };
    let _c = create();
    let _d = PublicTupleStruct(1, 2);
}

函数

module main;

import std.stdio;

int add( int x , int y )
{
    return x + y;
}

void main()
{
    // writeln( add( 42 , 13 ) );
    writeln( 42.add( 13 ) );
}

//锈
fn add(x: i32, y: i32) -> i32 {
    x + y
}

fn main() {
    println!("{}", add(42, 13));
}

不允许按方法使用函数,但反之成立.外部实现方法.

泛型

module main;

import std.stdio;

auto add( X , Y )( X x , Y y ) {
    return x + y; // 类型不匹配
}

void main()
{
    // writeln( 42.add!( int , float )( 13.3 ) );
    writeln( 42.add( 13.3 ) ); // 55.3
    writeln( 42.add( "WTF?" ) ); // 实例化出错
}

//锈
use std::ops::Add;

fn add<T1, T2, Result>(x: T1, y: T2) -> Result 
    where T1: Add<T2, Output = Result> {
    x + y
}

fn main() {
    println!("{}", add(42, 13));
    //println!("{}", add(42, "eee"));
    // 类型未实现`加`特征
}

为了"明确性"和更方便错误消息,牺牲了简洁性和部分灵活性.

多结果

module main;

import std.stdio;
import std.meta;
import std.typecons;

auto swap( Item )( Item[2] arg... )
{
    return tuple( arg[1] , arg[0] );
}

void main() 
{
    string a , b;
    AliasSeq!( a , b ) = swap( "hello" , "world" );
    writeln( a , b ); // worldhello
}

//锈
fn swap(a: i32, b: i32) -> (i32, i32) {
    (b, a)
}

fn main() {
    let (a, b) = swap(1, 2);
    //很像声明,完整匹配模式.
    println!("a is {} and b is {}", a, b);
}

都无命名返回值.

变量

module main;

import std.stdio;

void main() 
{
    bool c;
    bool python;
    bool java;
    int i;
}

//锈
fn main() {
    let c: bool;
    let python: bool;
    let java: bool;
    let i: i32;
}

中,编译器禁止访问未初化变量.D中默认都初化了.

短变量声明

//D
module main;

import std.stdio;

void main() 
{
    int i = 1 , j = 2;
    auto k = 3;
    auto c = true , python = false , java = "no!";

    writeln( i , j , k , c , python , java ); // 123truefalseno!
}

//锈
fn main() {
    let (i, j) = (1, 2);
    let k = 3;
    let (c, python, java) = (true, false, "no!");

    println!("{}, {}, {}, {}, {}, {}", i, j, k, c, python, java); // 1, 2, 3, true, false, no!
}

可从声明/使用中推导类型.

fn take_i8(_: i8) {}
fn take_i32(_: i32) {}

fn main() {
    let a = 10;
    let b = 20;

    take_i8(a);
    //take_i32(a); // error: 不匹配类型: expected `i32`, found `i8`

    take_i32(b);
    //take_i8(b); // error: 不匹配类型: expected `i8`, found `i32`
}

传递不完全限定的函数时,很方便.
基本类型略.
有张搞笑表

零值

//D
module main;
import std.stdio;
void main() 
{
    writefln( "%s %s %s \"%s\"" , int.init , double.init , bool.init , string.init ); // 0 nan false ""
}

//锈
fn main() {
    println!("{} {} {} '{}'", i32::default(), f64::default(), bool::default(), String::default()); // 0 0 false ''
}

转换类型

无隐式转换类型.

let a: i32 = 10;
let b: i64 = a as i64;

D提升规则.

posted @   zjh6  阅读(60)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示