• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 //! test for extern "C"
17 #![allow(clippy::approx_constant)]
18 use std::os::raw::c_double;
19 use std::os::raw::c_int;
20 
21 extern "C" {
abs(num: c_int) -> c_int22     fn abs(num: c_int) -> c_int;
sqrt(num: c_double) -> c_double23     fn sqrt(num: c_double) -> c_double;
pow(num: c_double, power: c_double) -> c_double24     fn pow(num: c_double, power: c_double) -> c_double;
25 }
26 
27 /// fn main()
main()28 fn main() {
29     let x: i32 = -123;
30     println!("This is an example of calling a C library function from Rust:");
31     println!("The absolute value of {x} is: {}.", unsafe { abs(x) });
32     let n: f64 = 9.0;
33     let p: f64 = 3.0;
34     println!("The {n}th power of {p} is: {}.", unsafe { pow(n, p) });
35     let mut y: f64 = 64.0;
36     println!("The square root of {y} is: {}.", unsafe { sqrt(y) });
37     y = -3.14;
38     println!("The square root of {y} is: {}.", unsafe { sqrt(y) });
39 }
40