CのRustに欠けているもの

著者について。 Federico Mena-Quinteroはメキシコのプログラマーであり、GNOMEプロジェクトの創設者の一人です

Librsvgは転換点に達しました。アクセサーを追加するよりも、CからRustにいくつかのコアパーツを移植する方が簡単であることが突然判明しました。 さらに、Rustではより多くの肉が記述されるようになりました。

今では2つの言語を頻繁に切り替える必要があり、Cは非常に原始的に見えます。

エレジーC


C 24 . “The C Programming Language by K&R” . Turbo Pascal, , C .

K&R — . malloc()/free(), . !

C. . , Unix 20 000 .

GIMP GTK+ . GNOME , . , 20 000 C .

. .

C


POV-Ray .

GTK+ , .

SIOD, Guile — , Scheme C.

Eye of GNOME .


Evolution . Solaris Purify; Valgrind.

gnome-vfs.

Mesa.

Nautilus-share , free().

, .

, .

, - Rust, C.


, Rust, « Rust ». Rust C++: « » (RAII), , , .


// C , , , ( , )… , .

( )


Vec<T> , T. . , T.

C … .

() —


Rust — Java- - . , Java — : Drawable, draw ().

.


() . , Iterator :

pub trait Iterator {
    type Item;
    fn next(&mut self) -> Option<Self::Item>;
}

, Iterator - Item . next() , Some(YourElementType). , None.

.

, Rust for , IntoIterator:

pub trait IntoIterator {
    /// The type of the elements being iterated over.
    type Item;

    /// Which kind of iterator are we turning this into?
    type IntoIter: Iterator<Item=Self::Item>;

    fn into_iter(self) -> Self::IntoIter;
}

Item , IntoIter — Iterator, .

, . « foo bar, , ».


C , , .


:


Cargo.toml, . c .

. cargo build.


- C :


Rust :

#[test]
fn test_that_foo_works() {
    assert!(foo() == expected_result);
}

cargo test, . . Makefile, , .

-.


Rust Markdown. . :

/// Multiples the specified number by two
///
/// ```
/// assert_eq!(multiply_by_two(5), 10);
/// ```
fn multiply_by_two(x: i32) -> i32 {
    x * 2
}

, .

23.02.2018: QuietMisdreavus , rustdoc doctests . .


Rust , C. , max (5 + 3, 4) .


C, - int short, char - — Rust . .


.

, Rust


Rust , « Rust» ( , unsafe {}) . >>  — .


gcc, switch() enum, ? .

Rust . match(). , :

impl f64 {
    pub fn sin_cos(self) -> (f64, f64);
}

let angle: f64 = 42.0;
let (sin_angle, cos_angle) = angle.sin_cos();

match() . .

let color = "green";

match color {
    "red"   => println!("it's red"),
    "green" => println!("it's green"),
    _       => println!("it's something else"),
}

?

my_func(true, false, false)

, :

pub struct Fubarize(pub bool);
pub struct Frobnify(pub bool);
pub struct Bazificate(pub bool);

fn my_func(Fubarize(fub): Fubarize, 
           Frobnify(frob): Frobnify, 
           Bazificate(baz): Bazificate) {
    if fub {
        ...;
    }

    if frob && baz {
        ...;
    }
}

...

my_func(Fubarize(true), Frobnify(false), Bazificate(true));

,


. boolean , , .

#[derive(Debug)]


(, ), #[derive(Debug)] — Rust , . , gdb .


user_data.


« », . , , .

C — . Unix, . .

Rust , . , , . , , .

Source: https://habr.com/ru/post/J350186/


All Articles